> For the complete documentation index, see [llms.txt](https://sathyakumars-kb.gitbook.io/user-authentication-based-django-app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sathyakumars-kb.gitbook.io/user-authentication-based-django-app/todo-app/task-list-user-authentication.md).

# Task List User Authentication

&#x20;Steps to Develop a Task List module.

## 1. Create template for TaskList module.

`templates/todo/task_list.html`

```python
{% 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 %}
```

## &#x20;2. Create view function for TaskList Module.

### &#x20;<mark style="color:green;">Remove the home view function and replace with below TaskList View</mark>

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:**

   ```python
   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:**

```python
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.

### <mark style="color:green;">Remove the home url and replace with below TaskList url</mark>

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:

```python
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.

#### &#x20;Output:

Without tasks data in model

<figure><img src="/files/wSQUUT6fwmz7BHb1afT3" alt=""><figcaption><p>Without Tasks data</p></figcaption></figure>

With tasks data in model

<figure><img src="/files/RSQhdNe5WC1H9cVH1cHx" alt=""><figcaption></figcaption></figure>
