{"id":160,"date":"2026-03-03T09:11:07","date_gmt":"2026-03-03T04:11:07","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=160"},"modified":"2026-03-17T08:50:15","modified_gmt":"2026-03-17T03:50:15","slug":"authentication-system","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/authentication-system\/","title":{"rendered":"Authentication System"},"content":{"rendered":"\n<p>An <strong>Authentication System<\/strong> is used to verify users and control access to different parts of a web application.<\/p>\n\n\n\n<p>Django provides a powerful built-in authentication system that handles:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User registration<\/li>\n\n\n\n<li>Login and logout<\/li>\n\n\n\n<li>Password hashing<\/li>\n\n\n\n<li>User sessions<\/li>\n\n\n\n<li>Permissions and access control<\/li>\n<\/ul>\n\n\n\n<p>You do not need to build authentication from scratch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Built-in User Model<\/h2>\n\n\n\n<p>Django includes a default <code>User<\/code> model with fields like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>username<\/li>\n\n\n\n<li>email<\/li>\n\n\n\n<li>password<\/li>\n\n\n\n<li>first_name<\/li>\n\n\n\n<li>last_name<\/li>\n\n\n\n<li>is_staff<\/li>\n\n\n\n<li>is_superuser<\/li>\n<\/ul>\n\n\n\n<p>It is located in:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.contrib.auth.models import User<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a Superuser<\/h2>\n\n\n\n<p>To access the admin panel:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python manage.py createsuperuser<\/pre>\n\n\n\n<p>Then log in at:<\/p>\n\n\n\n<p class=\"has-black-color has-text-color has-link-color wp-elements-fe9f5a5aeb4df73192f71caff5922172\"><a>http:\/\/127.0.0.1:8000\/admin\/<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Registering a New User<\/h2>\n\n\n\n<p>Example view to create a user:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.contrib.auth.models import User<br>from django.shortcuts import render, redirectdef register(request):<br>    if request.method == \"POST\":<br>        username = request.POST[\"username\"]<br>        password = request.POST[\"password\"]<br>        User.objects.create_user(username=username, password=password)<br>        return redirect(\"login\")<br>    return render(request, \"register.html\")<\/pre>\n\n\n\n<p><code>create_user()<\/code> automatically hashes the password for security.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Login System<\/h2>\n\n\n\n<p>Django provides authentication functions.<\/p>\n\n\n\n<p>Example login view:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.contrib.auth import authenticate, logindef user_login(request):<br>    if request.method == \"POST\":<br>        username = request.POST[\"username\"]<br>        password = request.POST[\"password\"]        user = authenticate(request, username=username, password=password)<br>        if user is not None:<br>            login(request, user)<br>            return redirect(\"home\")<br>    return render(request, \"login.html\")<\/pre>\n\n\n\n<p><code>authenticate()<\/code> checks credentials.<br><code>login()<\/code> creates a session for the user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Logout System<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.contrib.auth import logoutdef user_logout(request):<br>    logout(request)<br>    return redirect(\"login\")<\/pre>\n\n\n\n<p>Logout clears the user session.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Protecting Views (Login Required)<\/h2>\n\n\n\n<p>To restrict access to logged-in users:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.contrib.auth.decorators import login_required@login_required<br>def dashboard(request):<br>    return render(request, \"dashboard.html\")<\/pre>\n\n\n\n<p>If the user is not logged in, Django redirects to the login page.<\/p>\n\n\n\n<p>In settings.py:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">LOGIN_URL = \"login\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Checking Authentication in Template<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">{% if user.is_authenticated %}<br>    &lt;p&gt;Welcome, {{ user.username }}&lt;\/p&gt;<br>    &lt;a href=\"{% url 'logout' %}\"&gt;Logout&lt;\/a&gt;<br>{% else %}<br>    &lt;a href=\"{% url 'login' %}\"&gt;Login&lt;\/a&gt;<br>{% endif %}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Permissions and Groups<\/h2>\n\n\n\n<p>Django supports:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User permissions<\/li>\n\n\n\n<li>Groups<\/li>\n\n\n\n<li>Admin roles<\/li>\n<\/ul>\n\n\n\n<p>You can assign permissions through the admin panel.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Staff users<\/li>\n\n\n\n<li>Superusers<\/li>\n\n\n\n<li>Custom role-based access<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Password Security<\/h2>\n\n\n\n<p>Django automatically:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Hashes passwords<\/li>\n\n\n\n<li>Protects against common attacks<\/li>\n\n\n\n<li>Manages sessions securely<\/li>\n<\/ul>\n\n\n\n<p>Never store plain-text passwords.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Authentication is Important<\/h2>\n\n\n\n<p>Authentication helps:<\/p>\n\n\n\n<p>Secure user data<br>Protect sensitive pages<br>Control user access<br>Prevent unauthorized access<br>Build professional applications<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>Django\u2019s built-in authentication system makes it easy to manage users securely.<\/p>\n\n\n\n<p>It handles login, logout, registration, permissions, and password security \u2014 allowing you to focus on building features instead of implementing complex security systems from scratch.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773719558278\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FOR WEB DEVELOPMENT (PYWEB) > Django Framework > Authentication System<\/span><\/span><\/div>","protected":false},"menu_order":88,"template":"","class_list":["post-160","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Authentication System - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Django authentication: secure login, registration, logout, permissions, and user management using Django\u2019s built-in system efficiently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/authentication-system\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Authentication System - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Django authentication: secure login, registration, logout, permissions, and user management using Django\u2019s built-in system efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/authentication-system\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T03:50:15+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-system\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-system\\\/\",\"name\":\"Authentication System - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-03T04:11:07+00:00\",\"dateModified\":\"2026-03-17T03:50:15+00:00\",\"description\":\"Learn Django authentication: secure login, registration, logout, permissions, and user management using Django\u2019s built-in system efficiently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-system\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-system\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-system\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR WEB DEVELOPMENT (PYWEB) > Django Framework > Authentication System\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Authentication System - One Language. Endless Possibilities","description":"Learn Django authentication: secure login, registration, logout, permissions, and user management using Django\u2019s built-in system efficiently.","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:\/\/gigz.pk\/python\/lesson\/authentication-system\/","og_locale":"en_US","og_type":"article","og_title":"Authentication System - One Language. Endless Possibilities","og_description":"Learn Django authentication: secure login, registration, logout, permissions, and user management using Django\u2019s built-in system efficiently.","og_url":"https:\/\/gigz.pk\/python\/lesson\/authentication-system\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T03:50:15+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/authentication-system\/","url":"https:\/\/gigz.pk\/python\/lesson\/authentication-system\/","name":"Authentication System - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-03T04:11:07+00:00","dateModified":"2026-03-17T03:50:15+00:00","description":"Learn Django authentication: secure login, registration, logout, permissions, and user management using Django\u2019s built-in system efficiently.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/authentication-system\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/authentication-system\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/authentication-system\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR WEB DEVELOPMENT (PYWEB) > Django Framework > Authentication System"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}