gpt4 book ai didi

Django登录表单错误消息

转载 作者:行者123 更新时间:2023-12-04 18:33:01 25 4
gpt4 key购买 nike

我已经为我的 Django 项目创建了登录表单,但错误消息有问题。
这是我在 views.py 中的功能:

def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')

user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect('/friends_plans/users/')
else:
return HttpResponse("Your account is disabled")
else:
print "Invalid login details: {0}, {1}".format(username, password)
return HttpResponse("Invalid login details supplied.")
else:
return render(request, 'friends_plans/index.html', {})

错误消息(“提供的登录详细信息无效”)出现在新的空白页面上。如果在同一登录页面 (index.html) 上的用户名或密码不正确,您能告诉我如何显示此消息吗?

这是我的模板 index.html:
{% load staticfiles %}
<html >
<head >
<title> Friends' Plans </title>
<meta charset ="utf -8" />
<link rel="stylesheet" href="{% static 'css/friends_plans.css' %}">
</head >
<body >
<div id ="container">
<div id ="header">
<ul id ="menu">
<span><a id="firstbutton" href ="" >Friends' Plans</a> </span>
<span><a id="helpbutton" href ="" >HELP</a></span>
</ul>
</div>
<div id ="left">
<form id="login_form" method="post" action="">
{% csrf_token %}

Username: <input type ="text" name ="username" value="" size="50" /> <br />
Password: <input type ="password" name ="password" value="" size="50"/> <br />
<input type ="submit" value="submit" />
</form>
{% if user.is_authenticated %}
<a href="/friends_plans/logout/">Logout</a>
{% else %}
<a href="/friends_plans/register/">Register here</a><br />
{% endif %}
</div>
<div id ="right">
<h1 id="welcome">Welcome to Friends' Plans</h1>
<img class="cat" src={% static 'images/cat4.jpg' %} />
<img class="cat" src={% static 'images/cat2.jpg' %} />
<img class="cat" src={% static 'images/cat3.jpg' %} />
<img class="cat" src={% static 'images/cat6.jpg' %} />
<img class="cat" src={% static 'images/cat5.jpg' %} />
<img class="cat" src={% static 'images/cat1.jpg' %} />
</div>
<div id ="footer"> Copyright </div>
</div>
</body>
</html>

我有一个想法来设置一个变量 error=False并更改 if for error=True如果 form.is_valid() 为 False,并写入 {% if error %}在模板中,虽然有 {% csrf_token %},但是关于csrf_token却有错误在模板中。

最佳答案

在views.py中:

def login_process(request):
try:
user = authenticate(username = request.POST['username'],
password = request.POST['password'])
except KeyError:
return render(request, 'friends_plans/index.html',{
'login_message' : 'Fill in all fields',})
if user is not None:
#'User' and 'Pass' right
if user.is_active:
login(request, user)
else:
return render(request, 'friends_plans/index.html',{
'login_message' : 'The user has been removed',})
else:
return render(request, 'friends_plans/index.html',{
'login_message' : 'Enter the username and password correctly',})
return HttpResponseRedirect('/friends_plans/users/')

在 index.html 中:
{% load staticfiles %}
<html >
<head >
<title> Friends' Plans </title>
<meta charset ="utf -8" />
<link rel="stylesheet" href="{% static 'css/friends_plans.css' %}">
</head >
<body >
<div id ="container">
<div id ="header">
<ul id ="menu">
<span><a id="firstbutton" href ="" >Friends' Plans</a> </span>
<span><a id="helpbutton" href ="" >HELP</a></span>
</ul>
</div>
<div id ="left">
<form id="login_form" method="post" action="">
<div class="add_your_styles">
{{ login_message }}
</div>
{% csrf_token %}
Username: <input type ="text" name ="username" value="" size="50" /> <br />
Password: <input type ="password" name ="password" value="" size="50"/> <br />
<input type ="submit" value="submit" />
</form>
{% if user.is_authenticated %}
<a href="/friends_plans/logout/">Logout</a>
{% else %}
<a href="/friends_plans/register/">Register here</a><br />
{% endif %}
</div>
<div id ="right">
<h1 id="welcome">Welcome to Friends' Plans</h1>
<img class="cat" src={% static 'images/cat4.jpg' %} />
<img class="cat" src={% static 'images/cat2.jpg' %} />
<img class="cat" src={% static 'images/cat3.jpg' %} />
<img class="cat" src={% static 'images/cat6.jpg' %} />
<img class="cat" src={% static 'images/cat5.jpg' %} />
<img class="cat" src={% static 'images/cat1.jpg' %} />
</div>
<div id ="footer"> Copyright </div>
</div>
</body>
</html>

您可以将 {{ login message }} 放在任何地方。它只是一个文本字符串。

关于Django登录表单错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37124768/

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