I know it is basic to create login User Authentication. And I am a newbie here in using Django.
I have a problem in creating User Authentication:
Views.py
def Logins(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect('music/login')
return HttpResponseRedirect('music/login')
form = Userlogin
return render(request, 'music/login.html', {'Login_form': Userlogin})
urls.py url(r'^login/$', views.Logins, name='login'),
It shows It shows MultiValueDictKeyError at /music/login/. "'username'" Request Method: POST Request URL: http://127.0.0.1:8000/music/login/ Django Version: 1.10.4 Exception Type: MultiValueDictKeyError Exception Value:
"'username'"
login.html
{% block body %}
{% if form.errors %}
<p>Something is wrong</p>
{% endif %}
<form action="" method="post">
{% csrf_token %}
<label for="email">Login:</label>
<label for="password">Password:</label>
<input type="password" name="password" value="" id="username">
<input type="submit" value="login" />
</form>
{% endblock %}
Thanks in advance.
The MultiValueDictKeyError
seems to be because request.POST['username']
does not exist.
Add a username field in your login form.
<form action="" method="post">
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" name="username">
<label for="email">Login:</label>
<label for="password">Password:</label>
<input type="password" name="password" value="" id="username">
<input type="submit" value="login" />
</form>
request.POST
is a dictionary. You should be doing it this way:
username = request.POST.get('username', None)
password = request.POST.get('password', None)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.