简体   繁体   中英

How can I use django.auth.views.login?

How can I use django.auth.views.login? I am trying to make a user login but I can't figure out how to use it. Is there a thing you add to urls.py. I have an intermediate understanding of django. Thanks in advance!

For a quick and simple login view you just have to add the following to your urlpatterns in your urls.py file (You can change the regex to match whatever location you need it to match):

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

Next, add a directory called "registration" in your main templates folder, and within that create an html file called "login.html". This is the file that will be rendered with the login form. You can customize it to your hearts content, but here's a basic version:

<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>

And that should do it. Obviously, there's a lot more you can do there but that should get you started. For more details you can go here: http://docs.djangoproject.com/en/dev/topics/auth/#authentication-in-web-requests

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM