很抱歉,如果这是一个菜鸟问题。我正在为Django应用程序创建一个登录表单,但是我无法让它运行起来。 request.POST.get()不返回任何内容,因此身份验证始终失败。我错过了一些明显的东西吗?
的login.html:
{% extends "base.html" %}
{% block content%}
<h2>Welcome! Please login to continue.</h2> <br>
<form action="{% url 'account:authenticate' %}" method="post">
{% csrf_token %}
<div >
<label for='username'> Username: </label>
<input type='text' name='Username' id='username'> <br><br>
<label for='password'>Password: </label>
<input type='password' name='Password' id='password'><br><br>
<input type='submit' value='Login'>
</div>
</form>
views.py的相关部分:
from django.shortcuts import render, get_object_or_404
from datetime import datetime
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import loader
from random import randrange
from django.contrib.auth import authenticate
def login(request):
return render (request, 'login.html')
def authenticate(request):
usern = request.POST.get('username', '')
passw = request.POST.get('password', '')
user = authenticate(username = usern, password = passw)
if user is not None:
authenticate.login(request, user)
return HttpResponseRedirect('/voters/instructions')
else:
return HttpResponseRedirect('/account/loginfail')
def loginfail(request):
return render (request, 'loginfail.html')
我正在使用Django 1.10。谢谢!
答案 0 :(得分:4)
检查表单输入字段中的名称是否区分大小写。在Django做这个
usern = request.POST.get('Username', '')
passw = request.POST.get('Password', '')
或以html格式将它们设为小写输入名称字段