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

SETTINGS<\/strong><\/h3>\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’.
\n<\/strong><\/p>\n

LOGIN TEMPLATE<\/strong><\/h3>\n

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

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

To output that the user is logged in within your main base template you can use the following syntax,<\/p>\n

<p>Welcome, {{ user.username }}.<\/p><\/pre>\n

URLS<\/strong><\/h3>\n

Next some simple additions are made to the urls.py file.<\/p>\n

from django.conf.urls.defaults import patterns, include, url<\/pre>\n

urlpatterns = patterns(”,
\nurl(r’^main\/$’, ‘example.views.main’),
\n(r’^login\/$’, ‘example.views.login_user’),
\n)<\/p>\n

VIEWS<\/strong><\/h3>\n

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

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

def login_user(request):
\nlogout(request)
\nusername = password = ”
\nif request.POST:
\nusername = request.POST[‘username’]
\npassword = request.POST[‘password’]<\/p>\n

user = authenticate(username=username, password=password)
\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

@login_required(login_url=’\/login\/’)
\ndef main(request):
\n….<\/p>\n

 <\/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":"\nDjango - How do I create a custom login page ? - Fir3net<\/title>\n<meta name=\"description\" content=\"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\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Django - How do I create a custom login page ? - Fir3net\" \/>\n<meta property=\"og:description\" content=\"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\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html\" \/>\n<meta property=\"og:site_name\" content=\"Fir3net\" \/>\n<meta property=\"article:published_time\" content=\"2013-06-01T00:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-24T08:05:10+00:00\" \/>\n<meta name=\"author\" content=\"Rick Donato\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rick Donato\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html\"},\"author\":{\"name\":\"Rick Donato\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\"},\"headline\":\"Django – How do I create a custom login page ?\",\"datePublished\":\"2013-06-01T00:00:00+00:00\",\"dateModified\":\"2023-02-24T08:05:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html\"},\"wordCount\":347,\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"articleSection\":[\"Django\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html\",\"url\":\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html\",\"name\":\"Django - How do I create a custom login page ? - Fir3net\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/#website\"},\"datePublished\":\"2013-06-01T00:00:00+00:00\",\"dateModified\":\"2023-02-24T08:05:10+00:00\",\"description\":\"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\",\"breadcrumb\":{\"@id\":\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.fir3net.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming\",\"item\":\"https:\/\/www.fir3net.com\/programming\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Web Development\",\"item\":\"https:\/\/www.fir3net.com\/programming\/web-development\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Django\",\"item\":\"https:\/\/www.fir3net.com\/programming\/web-development\/django\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Django – How do I create a custom login page ?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.fir3net.com\/#website\",\"url\":\"https:\/\/www.fir3net.com\/\",\"name\":\"Fir3net\",\"description\":\"Keeping you in the know\",\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.fir3net.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.fir3net.com\/#organization\",\"name\":\"Fir3net\",\"url\":\"https:\/\/www.fir3net.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"contentUrl\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"width\":390,\"height\":88,\"caption\":\"Fir3net\"},\"image\":{\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\",\"name\":\"Rick Donato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"caption\":\"Rick Donato\"},\"description\":\"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Django - How do I create a custom login page ? - Fir3net","description":"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","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html","og_locale":"en_US","og_type":"article","og_title":"Django - How do I create a custom login page ? - Fir3net","og_description":"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","og_url":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html","og_site_name":"Fir3net","article_published_time":"2013-06-01T00:00:00+00:00","article_modified_time":"2023-02-24T08:05:10+00:00","author":"Rick Donato","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rick Donato","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html#article","isPartOf":{"@id":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html"},"author":{"name":"Rick Donato","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037"},"headline":"Django – How do I create a custom login page ?","datePublished":"2013-06-01T00:00:00+00:00","dateModified":"2023-02-24T08:05:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html"},"wordCount":347,"publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"articleSection":["Django"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html","url":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html","name":"Django - How do I create a custom login page ? - Fir3net","isPartOf":{"@id":"https:\/\/www.fir3net.com\/#website"},"datePublished":"2013-06-01T00:00:00+00:00","dateModified":"2023-02-24T08:05:10+00:00","description":"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","breadcrumb":{"@id":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.fir3net.com\/Web-Development\/Django\/django.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.fir3net.com\/"},{"@type":"ListItem","position":2,"name":"Programming","item":"https:\/\/www.fir3net.com\/programming"},{"@type":"ListItem","position":3,"name":"Web Development","item":"https:\/\/www.fir3net.com\/programming\/web-development"},{"@type":"ListItem","position":4,"name":"Django","item":"https:\/\/www.fir3net.com\/programming\/web-development\/django"},{"@type":"ListItem","position":5,"name":"Django – How do I create a custom login page ?"}]},{"@type":"WebSite","@id":"https:\/\/www.fir3net.com\/#website","url":"https:\/\/www.fir3net.com\/","name":"Fir3net","description":"Keeping you in the know","publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.fir3net.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.fir3net.com\/#organization","name":"Fir3net","url":"https:\/\/www.fir3net.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","contentUrl":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","width":390,"height":88,"caption":"Fir3net"},"image":{"@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037","name":"Rick Donato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","caption":"Rick Donato"},"description":"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders."}]}},"_links":{"self":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/776"}],"collection":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/comments?post=776"}],"version-history":[{"count":2,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/776\/revisions"}],"predecessor-version":[{"id":3642,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/776\/revisions\/3642"}],"wp:attachment":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/media?parent=776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/categories?post=776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/tags?post=776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}