T
T
The-TS2021-02-03 20:56:52
Django
The-TS, 2021-02-03 20:56:52

What's wrong with Django?

Hey!

Problem
Environment:


Request Method: POST
Request URL: http://192.168.0.107:8000/TSP_app/

Django Version: 3.1.5
Python Version: 3.9.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'polls.apps.PollsConfig',
 'accounts',
 'TSP_app',
 'rest_framework',
 'API_TSP',
 'bootstrap5',
 'TSP_server']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/home/ts/Dev/Django/mysite/Django_app/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/ts/Dev/Django/mysite/Django_app/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/ts/Dev/Django/mysite/TSP_app/views.py", line 27, in home
    'private_key': gpg.export_keys(key, True, passphrase=passphrase),
  File "/home/ts/Dev/Django/mysite/Django_app/lib/python3.9/site-packages/gnupg.py", line 1306, in export_keys
    self._handle_io(args, f, result, passphrase=passphrase,
  File "/home/ts/Dev/Django/mysite/Django_app/lib/python3.9/site-packages/gnupg.py", line 1028, in _handle_io
    p = self._open_subprocess(args, passphrase is not None)
  File "/home/ts/Dev/Django/mysite/Django_app/lib/python3.9/site-packages/gnupg.py", line 934, in _open_subprocess
    result = Popen(cmd, shell=False, stdin=PIPE, stdout=PIPE, stderr=PIPE,
  File "/usr/lib/python3.9/subprocess.py", line 947, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.9/subprocess.py", line 1752, in _execute_child
    self.pid = _posixsubprocess.fork_exec(

Exception Type: TypeError at /TSP_app/
Exception Value: expected str, bytes or os.PathLike object, not GenKey

Problem code:
from django.shortcuts import render
from django.http import JsonResponse
from .forms import create_key_form
import gnupg

gpg = gnupg.GPG()
gpg.encoding = 'utf-8'

def home(request):
    if request.method == 'POST':
        form = create_key_form(request.POST)
        if form.is_valid():
            key_type = form.cleaned_data['key_type']
            key_lenght = form.cleaned_data['key_lenght']

            real_name = form.cleaned_data['real_name']
            comment = form.cleaned_data['comment']
            passphrase = form.cleaned_data['passphrase']

            save_key = form.cleaned_data['save_key']

            input_data = gpg.gen_key_input(key_type=key_type, key_length=key_lenght, name_real=real_name, comment=comment, passphrase=passphrase)
            key = gpg.gen_key(input_data)

            return render(request, 'TSP_app', {
                'have_key': True,
                'private_key': gpg.export_keys(key, True, passphrase=passphrase), # <-- Здесь
                'public_key': gpg.export_keys(key)
            })

    form = create_key_form()

    return render(request, 'TSP_app/home.html', {'form': form})


Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Koval, 2021-06-16
@vvkSeven

Error in views on line 27, specifically with return render. Django expects you to enter a string. Try to debug the print and make sure that there is really a string there, and not just a private_key generation. As a last resort, add print(type()) to find out what type it is, and convert it to a string, it should help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question