User Authentication Based Django App
  • Python Django
  • Basic Python Virtual Environment Setup
  • Project Set Up
  • Model-View-Templates Implementation
  • Model & Django Admin Area
  • View
    • Types of Views in Django
    • Project Todo Views
  • Django Templates
  • Todo App
    • DEMO: Workflow of Todo App
    • Base Template for Todo App
    • Registration Module
    • Login Module
    • Linking Login and Registration page
    • Task List User Authentication
    • Task Reordering
    • Logout Module
    • Task Create Module
    • Task Update
    • Task Delete
Powered by GitBook
On this page
  • 1. Create template for TaskList module.
  • 2. Create view function for TaskList Module.
  • Remove the home view function and replace with below TaskList View
  • 3. Mapping URLs to login view.
  • Remove the home url and replace with below TaskList url
  • 4. Run the development server to check the functionality:
  1. Todo App

Task List User Authentication

Steps to Develop a Task List module.

1. Create template for TaskList module.

templates/todo/task_list.html

{% extends 'todo/main.html' %} {% block content %}

<div class="header-bar">
    <div>
        <h1>Hello {{request.user|title}}</h1>
        <h3 style="margin:0">You have <i>{{count}}</i> incomplete task{{ count|pluralize:"s" }}</h3>
    </div>

    {% if request.user.is_authenticated %}
    <a href="">Logout</a> {% else %}
    <a href="{% url 'login' %}">Login</a> {% endif %}
</div>


<div id="search-add-wrapper">
    {% if tasks|length > 0 %}
    <a id="add-link" href="">&#x2b;</a>
    {% endif %}
</div>

<div id="tasklist" class="task-items-wrapper">
    {% for task in tasks %}
    <div class="task-wrapper" data-position="{{task.pk}}">
        <div class="task-title">
            {% if task.complete %}
            <div class="task-complete-icon"></div>
            <i><s><a href="">{{task}}</a></s></i> {% else %}
            <div class="task-incomplete-icon"></div>
            <a href="">{{task}}</a> {% endif %}
        </div>
        <div class="task-controls">
            <a class="delete-link" href="">&#215;</a>
            <span class="handle">&nbsp;&#10247;</span>
        </div>
    </div>

    {% empty %}
    <div style="text-align: center; padding-bottom: 10px; line-height: 1em;">
        <h3>No new tasks are created.</h3>
        <h3>Create a <a style="text-decoration: none; color: #e53935;" href="">New task</a> ! </h3>
    </div>
    {% endfor %}
</div>

{% endblock content %}

2. Create view function for TaskList Module.

Remove the home view function and replace with below TaskList View

you've defined a Django class-based view using the ListView and LoginRequiredMixin mixins. This view is designed to display a list of tasks from the Task model. Here's a breakdown of your code:

  1. Import Statements:

    from django.views.generic.list import ListView
    from django.contrib.auth.mixins import LoginRequiredMixin

    These statements import the necessary classes for creating a ListView and applying the LoginRequiredMixin.

  2. Class Definition:

class TaskList(LoginRequiredMixin, ListView):
    model = Task
    template_name = 'todo/task_list.html'
    context_object_name = 'tasks'
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['tasks'] = context['tasks'].filter(user=self.request.user)
        context['count'] = context['tasks'].filter(complete=False).count()

        return context
  • TaskList is your class-based view, inheriting from LoginRequiredMixin and ListView.

  • model = Task specifies the model from which the view retrieves the list of tasks.

  • template_name = 'todo/task_list.html' sets the template used to render the view. This assumes you have a template at 'todo/task_list.html'.

  • context_object_name = 'tasks' sets the variable name used in the template to access the list of tasks.

  • Django class-based view and using the get_context_data method to add additional context to your template.

    1. super().get_context_data(**kwargs): This line calls the get_context_data method of the parent class, which is usually a mixin or a base class for your view. It retrieves the initial context.

    2. context['tasks'] = context['tasks'].filter(user=self.request.user): This line filters the tasks based on the current user. It assumes that context['tasks'] is a queryset of tasks, and it filters them to include only those associated with the currently logged-in user (self.request.user).

    3. context['count'] = context['tasks'].filter(complete=False).count(): This line counts the number of incomplete tasks in the filtered queryset. It adds a new key 'count' to the context with the count value.

    4. The modified context dictionary is then returned, and it will be used to render the template with the additional data.

The LoginRequiredMixin ensures that only authenticated users can access this view. If a user is not logged in, they will be redirected to the login page.

This code assumes that you have a Django model named Task and a corresponding template at 'todo/task_list.html' to display the list of tasks.

3. Mapping URLs to login view.

Remove the home url and replace with below TaskList url

Make sure you have the necessary URLs and other configurations in place to use this custom TaskList view. For example, in your urls.py file, you might have something like:

from django.urls import path
from .views import TaskList

urlpatterns = [
    path('', TaskList.as_view(), name='tasks'),
    # Add other URL patterns as needed
]

This would map the TaskList view to the '/' URL, and you can use the name 'tasks' to refer to this URL in your templates or other parts of your Django application.

4. Run the development server to check the functionality:

Start the development server if it's not running already:

python manage.py runserver

Access the interface by visiting http://localhost:8000 in your web browser.

Output:

Without tasks data in model

With tasks data in model

PreviousLinking Login and Registration pageNextTask Reordering

Last updated 1 year ago

Without Tasks data