Introduction Logout user Change password

User login

This assumes that the user app has already been created (see introduction)

User login

To support tabindex in the login we need to create a Django filter

  1. In the home directory create a directory call templatetags with an init.py

  2. in templatetags create app_filters.py:

     from django import template
    
     register = template.Library()    
    
     @register.filter
     def tabindex(value, index):
         """Add a tabindex attribute to the widget for a bound field."""
         value.field.widget.attrs['tabindex'] = index
         return value
    

    This will be used in the template (see below).

  3. In forms.py

     from django import forms
     from django.contrib.auth.models import User
    
    
     class LoginForm(forms.ModelForm):
         username = forms.CharField(label='User name', max_length=30)
         password = forms.PasswordInput()
    
         class Meta:
             model = User
             fields = '__all__'
    
             widgets = {'password': forms.PasswordInput()}
    
    
  4. In users/urls.py:

     urlpatterns = [
             ...
             path(r'login/', views.login_view, name='login'),
         ]
    
  5. In views.py

    (This assumes the import statements in logout have already been inserted.)

     from django.http import HttpResponseRedirect
    
     def login_view(request):
         form_context = {'login_form': LoginForm,}
         url = "users/login.html"
         if request.method == '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(RETURN_FROM_LOGIN_URL)
                 else:
                     messages.error(request, 'Your account is not enabled!')
                 context = {}
             else:
                 messages.error(request, 'Your username or password was not recognized!')
                 context = form_context
         else:
             context = form_context
         return render(request, url, context)
    


  6. And in login.html

     {% extends "base.html" %}
     {% load app_filters %}
     {% block content %}
         <br>
         {% if form.errors %}
             <p>Your username and password didn't match. Please try again.</p>
         {% endif %}
    
         {% if next %}
             {% if user.is_authenticated %}
                 <p>Your account doesn't have access to this page. To proceed,
                 please login with an account that has access.</p>
             {% else %}
                 <p>Please login to see this page.</p>
             {% endif %}
         {% endif %}
    
         <form method="post" action="{% url 'login' %}">
         {% csrf_token %}
             <table>
                 <tr>
                     <td></td>
                     <td><div></div></td>
                     <td rowspan="2"><div tabindex="3"><button type="submit" class="btn btn-success">Login</button></div></td>
                 </tr>
                 <tr>
                     <td></td>
                     <td><div></div></td>
                 </tr>
             </table>
         </form>
         {% if messages %}
             <ul class="messages">
                 {% for message in messages %}
                 <li {% if message.tags %} class=""{% endif %}></li>
                 {% endfor %}
             </ul>
         {% endif %}
     {% endblock %}
    


Introduction Logout user Change password