gpt4 book ai didi

python - 我如何使用 django.auth.views.login?

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:05 25 4
gpt4 key购买 nike

如何使用 django.auth.views.login?我正在尝试让用户登录,但我不知道如何使用它。有没有你添加到 urls.py 的东西。我对django有一个中等的了解。提前致谢!

最佳答案

对于快速简单的登录 View ,您只需将以下内容添加到您的 urls.py 文件中的 urlpatterns(您可以更改正则表达式以匹配您需要匹配的任何位置):

(r'^login/$', 'django.contrib.auth.views.login'),

接下来,在您的主模板文件夹中添加一个名为“registration”的目录,并在其中创建一个名为“login.html”的 html 文件。这是将与登录表单一起呈现的文件。您可以根据自己的喜好对其进行自定义,但这是一个基本版本:

<html>
<head> <title>User Login</title> </head>
<body>
<h1>User Login</h1>
{% if form.errors %}
<p>Your username and password didn't match!
Please try again!</p>
{% endif %}
<form method="post" action=".">
{% csrf_token %}
<p><label for="id_username">Username:</label>{{ form.username }}</p>
<p><label for="id_password">Password:</label>{{ form.password }}</p>
<input type="hidden" name="next" value="/" />
<input type="submit" value="login" />
</form>
</body>
</html>

那应该就可以了。显然,您可以在那里做更多的事情,但这应该可以帮助您入门。有关更多详细信息,您可以访问此处:http://docs.djangoproject.com/en/dev/topics/auth/#authentication-in-web-requests

关于python - 我如何使用 django.auth.views.login?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5643358/

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