gpt4 book ai didi

python - Django Form request.POST.get() 总是返回空

转载 作者:太空狗 更新时间:2023-10-30 00:01:48 25 4
gpt4 key购买 nike

抱歉,如果这是一个菜鸟问题。我正在为 Django 应用程序创建一个登录表单,但我无法让它工作。 request.POST.get() 不返回任何内容,因此身份验证总是失败。我是否遗漏了一些明显的东西?

登录.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。谢谢!

最佳答案

检查表单输入字段中区分大小写的名称。在 Django 中执行此操作

 usern = request.POST.get('Username', '')
passw = request.POST.get('Password', '')

或者在 html 格式中将输入名称字段设为小写

关于python - Django Form request.POST.get() 总是返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39940182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com