'username'
Request Method: | GET |
---|---|
Request URL: | http://127.0.0.1:8000/login/ |
Django Version: | 2.2.3 |
Exception Type: | MultiValueDictKeyError |
Exception Value: | 'username' |
Exception Location: | C:\Python\Python37\lib\site-packages\django\utils\datastructures.py in __getitem__, line 80 |
Python Executable: | C:\Python\Python37\python.exe |
Python Version: | 3.7.3 |
rom django.shortcuts import render
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from .forms import RegisterForm
from django.conf import settings
from django.shortcuts import redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.contrib.auth import authenticate,login
from django.contrib import auth
from django.core.exceptions import ObjectDoesNotExist
def user_register(request):
# if this is a POST request we need to process the form data
template = 'mymodule/register.html'
# template = 'index.html'
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = RegisterForm(request.POST)
# check whether it's valid:
if form.is_valid():
if User.objects.filter(username=form.cleaned_data['username']).exists():
return render(request, template, {
'form': form,
'error_message': 'Username already exists.'
})
elif User.objects.filter(email=form.cleaned_data['email']).exists():
return render(request, template, {
'form': form,
'error_message': 'Email already exists.'
})
elif form.cleaned_data['password'] != form.cleaned_data['password_repeat']:
return render(request, template, {
'form': form,
'error_message': 'Passwords do not match.'
})
else:
# Create the user:
user = User.objects.create_user(
form.cleaned_data['username'],
form.cleaned_data['email'],
form.cleaned_data['password']
)
user.first_name = form.cleaned_data['first_name']
user.last_name = form.cleaned_data['last_name']
user.phone_number = form.cleaned_data['phone_number']
user.save()
return redirect('index.html')
# Login the user
#login(request, user)
#def user_login(request):
# redirect to accounts page:
#return render(request, '/login.html')
# return HttpResponseRedirect(return, '/login.html')
# No post data availabe, let's just show the page.
else:
form = RegisterForm()
return render(request, template, {'form': form})
def login_view(request):
username = request.POST['username']
password = request.POST['pass']
users = authenticate(request, username=username, password=password)
if users is not None:
login(request, users)
return redirect('welcome.html')
else:
return redirect('index.html')
def logout(request):
auth.logout(request)
return render(request, 'login.html')
> request.POST['username']
which will raise a KeyError
exception if 'username'
is not in request.POST
.Instead use -> request.POST.get('
which will return username
')None
if '
is not in username
'request.POST
.
Additionally, .get
allows you to provide an additional
parameter of a default value which is returned if the key is not in the
dictionary.
For example, request.POST.get('
username
', 'mydefaultvalue')
Hope this helps. If any concern then let us know.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BTqRsvD7P8Mk4RCUUg-5%3DYTE7PjBnHyJA%3DVZowGgGoYQnyNKw%40mail.gmail.com.
--
--
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMSP2afysMf%2BCiy2L8iRA2YejHvCQMon3JO3x5hd8bmWjuRAkw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGHZBzzRHrUvP7P1Aa-8qdsaDG65TBXUX54RJ8sxtU1G08c2Fg%40mail.gmail.com.
def login_view(request):
templates = 'welcome.html'
username = request.POST.get('username')
password = request.POST.get('pass')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return render(request, templates ,{'user' : user })
else:
#return redirect('welcome.html')
messages.error(request, 'Login Failed! Invalid username and password.')
return render(request, 'login.html',{'user' : user })