[Django]-How can I use django.auth.views.login?

8đź‘Ť

âś…

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

👤chandsie

Leave a comment