Это не весь мой код. Это часть моего кода. Это для вариации отп. Хотя я должен использовать random.radient()
для генерации случайного значения. Но это для тестирования.
Views.py
if otp == 1234:
user = User.objects.create_user(first_name=first_name, last_name=last_name, username=username,email=email, password=password1)
user.save()
messages.success(request, 'User Created')
return redirect('register')
print("User Created")
else:
messages.error(request, 'Otp does not matched')
return redirect('register')
регистр. html
<input type="text" class="form-control" name="otp" placeholder="Enter OTP" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign Up</button><br>
Выдает ошибку MultiValueDictKeyError
. Как устранить эту ошибку?
ошибка в этой строке otp = request.POST['otp']
. Я думаю.
КАК ЭТО РАЗРЕШИТЬ?
ПОЛНЫЙ ОТСЛЕЖИВАНИЕ
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/register
Django Version: 3.0.6
Python Version: 3.6.8
Installed Applications:
['accounts.apps.AccountsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
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 "C:\Users\Ujjwal Kar\Python\env\lib\site-packages\django\utils\datastructures.py", line 76, in __getitem__
list_ = super().__getitem__(key)
During handling of the above exception ('otp'), another exception occurred:
File "C:\Users\Ujjwal Kar\Python\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Ujjwal Kar\Python\env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Ujjwal Kar\Python\env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Ujjwal Kar\Python\Project\Doctormirror\accounts\views.py", line 43, in register
otp = request.POST['otp']
File "C:\Users\Ujjwal Kar\Python\env\lib\site-packages\django\utils\datastructures.py", line 78, in __getitem__
raise MultiValueDictKeyError(key)
Exception Type: MultiValueDictKeyError at /register
Exception Value: 'otp'