How To Create Custom Login Page In Django

Websolutionstuff | May-14-2022 | Categories : Python

In this article, we will see how to create a custom login page in django. how to allow user registration, login, and logout functionality. how to permit only authenticated users to a view via the use of a custom login form. Django by default provides an authentication system configuration but in this example, we will learn a custom login page in Django.

So, let's see Django create a custom login page, Django login, and registration, create a custom authentication Django, Django login form, custom login, and logout in Django.

In the settings.py file, we need some minor changes like the below code.

+ 'django.contrib.auth.middleware.AuthenticationMiddleware' to MIDDLEWARE_CLASSES
+ 'django.contrib.auth' and 'django.contrib.contenttypes'to INSTALLED_APPS

 

 

Once done update your database by running 'python manage.py syncdb'.

the custom login page is created via another template. In this case, we have named it login.html

Note: the CSS styling is bootstrap.

{% extends "website-base.html" %}
{% block main %}
    <div id="login">
        <form class="form-horizontal" name="LoginForm" action="/login/" method="post">
        {% csrf_token %}
        {% if next %}
            <input type="hidden" name="next" value="{{ next }}" />
        {% endif %}
        <div class="control-group">
            <label class="control-label" for="username">Username</label>
            <div class="controls">
                <input type="text" id="username" name="username"  placeholder="Username">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label" for="password">Password</label>
            <div class="controls">
                <input type="password" name="password" id="password" placeholder="Password">
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <button type="submit" class="btn">Login</button>
            </div>
        </div>
        </form>
    </div>
{% endblock %}

To output that the user is logged in within your main base template you can use the following syntax,

<p>Welcome, {{ user.username }}.</p>

Now add the URL in the urls.py file.

from django.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('',
    url(r'^main/$', 'example.views.main'),
    (r'^login/$', 'example.views.login_user'),
)

 

 

Finally, we build a new view. This will take the username and password from the POST and test them against the currently active users within Django's auth system. only authenticated users can access the view. So, we will use def main(request). This decorator is used if the user is not authenticated and sends them back to the login page.

from django.http import *
from django.shortcuts import render_to_response,redirect
from django.template import RequestContext
from birthdayreminder.models import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login, logout

def login_user(request):
    logout(request)
    username = password = ''
    if request.POST:
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/main/')
    return render_to_response('login.html', context_instance=RequestContext(request))

@login_required(login_url='/login/')
def main(request):
    ....

Note: 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.

 


You might also like :

Recommended Post
Featured Post
Laravel 9 Order By Query Example
Laravel 9 Order By Query Examp...

In this article, we will see laravel 9 order by query example. how to use order by in laravel 9.The orderBy me...

Read More

Mar-28-2022

How To Create Stacked Bar Chart In Laravel 9 Using Highcharts
How To Create Stacked Bar Char...

In this article, we will see how to create a dynamic stacked bar chart in laravel 9 using highchart. Here, we will...

Read More

Dec-30-2022

Laravel 8 Datatables Filter with Dropdown
Laravel 8 Datatables Filter wi...

In this example we will see laravel 8 datatables filter with dropdown, Here we will add datatables custom...

Read More

Jun-16-2021

Laravel 11 ChartJS Line Chart Example
Laravel 11 ChartJS Line Chart...

Hello developer! In this article, we'll see how to create a dynamic line chart in laravel 11 using chart js. Yo...

Read More

May-10-2024