{"id":776,"date":"2013-06-01T00:00:00","date_gmt":"2013-06-01T00:00:00","guid":{"rendered":"https:\/\/fir3netwp.gmsrrpobkbd.com\/2013\/06\/01\/django\/"},"modified":"2023-02-24T08:05:10","modified_gmt":"2023-02-24T08:05:10","slug":"django","status":"publish","type":"post","link":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html","title":{"rendered":"Django – How do I create a custom login page ?"},"content":{"rendered":"
Within this article we will look at how to permit only authenticated users to a view via the use of a custom login form.<\/p>\n
First of all a few changes need to be made to the settings.py file. Such as<\/p>\n
+\u00a0‘django.contrib.auth.middleware.AuthenticationMiddleware’ to MIDDLEWARE_CLASSES
\n+ ‘django.contrib.auth’ and ‘django.contrib.contenttypes’to INSTALLED_APPS<\/p>\n
Once done update your database by running ‘python manage.py syncdb’. Next the custom login page is created via another template. In this case we have named it login.html.<\/p>\n Note<\/em> : the CSS styling is bootstrap<\/a> based.<\/p>\n To output that the user is logged in within your main base template you can use the following syntax,<\/p>\n Next some simple additions are made to the urls.py file.<\/p>\n urlpatterns = patterns(”, Finally we build a new view. This will take the username and password from the POST and test them against the current active users within Django’s auth system.<\/p>\n The main point here is that to ensure that only authenticated users can access the view (in this case ‘def main(request)’) a decorator<\/a> is used. This decorator<\/a> also dictates that if the user is not authenticated to send then back to the login page.<\/p>\n Note<\/em> : The reason ‘logout(request)’ is added to the top of the view is so that if you ever go to the login.html page directly then the user is logged out. Typically this would be achieved by creating a separate logout page but (in this example) to keep things simple we have included this within the login view.<\/p>\n def login_user(request): user = authenticate(username=username, password=password) @login_required(login_url=’\/login\/’) <\/p>\n","protected":false},"excerpt":{"rendered":" Within this article we will look at how to permit only authenticated users to a view via the use of a custom login form. SETTINGS First of all a few changes need to be made to the settings.py file. Such as +\u00a0‘django.contrib.auth.middleware.AuthenticationMiddleware’ to MIDDLEWARE_CLASSES + ‘django.contrib.auth’ and ‘django.contrib.contenttypes’to INSTALLED_APPS Once done update your database by … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"yoast_head":"\n
\n<\/strong><\/p>\nLOGIN TEMPLATE<\/strong><\/h3>\n
{% extends \"website-base.html\" %}\r\n{% block main %}\r\n\u00a0\u00a0\u00a0 <div id=\"login\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<form class=\"form-horizontal\" name=\"LoginForm\" action=\"\/login\/\" method=\"post\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{% csrf_token %}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{% if next %}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<input type=\"hidden\" name=\"next\" value=\"{{ next }}\" \/>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{% endif %}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<div class=\"control-group\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<label class=\"control-label\" for=\"username\">Username<\/label>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<div class=\"controls\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<input type=\"text\" id=\"username\" name=\"username\"\u00a0 placeholder=\"Username\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<\/div>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<\/div>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<div class=\"control-group\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<label class=\"control-label\" for=\"password\">Password<\/label>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<div class=\"controls\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<input type=\"password\" name=\"password\" id=\"password\" placeholder=\"Password\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<\/div>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<\/div>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<div class=\"control-group\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<div class=\"controls\">\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<button type=\"submit\" class=\"btn\">Login<\/button>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<\/div>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<\/div>\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<\/form>\r\n\u00a0\u00a0\u00a0 <\/div>\r\n{% endblock %}<\/pre>\n
<p>Welcome, {{ user.username }}.<\/p><\/pre>\n
URLS<\/strong><\/h3>\n
from django.conf.urls.defaults import patterns, include, url<\/pre>\n
\nurl(r’^main\/$’, ‘example.views.main’),
\n(r’^login\/$’, ‘example.views.login_user’),
\n)<\/p>\nVIEWS<\/strong><\/h3>\n
from django.http import *\r\nfrom django.shortcuts import render_to_response,redirect\r\nfrom django.template import RequestContext\r\nfrom birthdayreminder.models import *\r\nfrom django.contrib.auth.decorators import login_required\r\nfrom django.contrib.auth import authenticate, login, logout<\/pre>\n
\nlogout(request)
\nusername = password = ”
\nif request.POST:
\nusername = request.POST[‘username’]
\npassword = request.POST[‘password’]<\/p>\n
\nif user is not None:
\nif user.is_active:
\nlogin(request, user)
\nreturn HttpResponseRedirect(‘\/main\/’)
\nreturn render_to_response(‘login.html’, context_instance=RequestContext(request))<\/p>\n
\ndef main(request):
\n….<\/p>\n