Django: Simple User Authentication Login and Logout. This problem hasbeen asked with someone in in, he asked “how to custom template of login form in django”.

First, makesure you understrand how django is work. and in this method, we used default model User from django. and default user authentications from django. There just modified template for login page.

There we use Django1.8++

1. In your, create some function for viewing user authentications with some conditions.

from django.contrib.auth import logout
from django.shortcuts import redirect

class User_Authentication_Views(object):
    #def registration_view(self, request):
    #    #do_stuff with form and save into your model or whatever you need.

    def logout_view(self, request):
        return redirect('/login/')

2. In your, customing default django.contrib.auth.views.login with your template.

from django.conf.urls import include, url
from . import views

urlpatterns = [
    #url user authentication
    url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login_form.html'}, name='login_page'),
    url(r'^logout/$', views.User_Authentication_Views().logout_view, name='logout_page'),

3. In your templates/login_form.html, you can call your form with {{ form.as_p }}{{ form.as_table }}, or if you need to specific egg like this: {{ form.username }} and {{ form.password }} –> it just showing egg: ``, please checkout in this docs for more:

{% extends 'base.html' %}
{% block content %}

    {% if form.errors %}

           Upps, Username and Password didn't match!

    {% endif %}

    {% csrf_token %}
        {{ form.as_p }}

{% endblock %}


Hope it usefull..

146 7 7 0