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
  • Note
  • Step 1. Develop Templates:
  • Step 2. Mapping URLs to views (urls.py):
  • Step 3. Create Views (views.py):
  • Step 4: Run the development server to check the functionality:
  1. Todo App

DEMO: Workflow of Todo App

Demonstrating the workflow for Todo App

Note

This documentation outlines the key steps in creating a ToDo app using Django, including defining URL patterns, creating views to handle logic, and developing templates for user interface presentation.

Step 1. Develop Templates:

create templates/todo folder

Create HTML templates to define the structure and layout of your ToDo app's pages. Templates are used by views to generate dynamic content that is sent to the user's browser.

save main.html in templates/todo folder

main.html:

<!-- Example main.html -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {% block content %}

    {%endblock content%}
</body>
</html>

save task.html in templates/todo folder

task.html:

<!-- Example task.html -->

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

{% block content %}
<h1>Task List</h1>

{% for task in tasks%}
<h3>{{task}}</h3>
{% endfor%}

{% endblock content %}

Step 2. Mapping URLs to views (urls.py):

Begin by establishing the URL patterns for your Todo app. This involves mapping specific URLs to corresponding views that will handle the user's requests.

# Example urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('tasks', views.Tasks_list.as_view(), name='tasks'),
    # Add more URL patterns as needed
]

Step 3. Create Views (views.py):

Develop the views that will handle the logic for different parts of your ToDo app. Views are responsible for processing user requests, interacting with the data model, and rendering templates.

from django.views.generic.list import ListView

class Tasks_list(ListView):
    model = Task
    template_name = 'todo/task.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)
        return context

Here's a breakdown of how this works:

  1. self.request.user: This assumes that you are inside a Django class-based view or a middleware where self.request is an instance of the HttpRequest object, and user is the user associated with the request.

  2. context['tasks']: This suggests that you are working with a context dictionary, commonly used in Django views to pass data to templates.

  3. context['tasks'].filter(...): It implies that context['tasks'] is a queryset, likely a model queryset, on which you want to apply a filter.

  4. .filter(user=self.request.user): This is filtering the queryset to include only those records where the user field is equal to the self.request.user. This is a common pattern to filter objects based on the currently logged-in user.

Step 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/tasks in your web browser.

PreviousDjango TemplatesNextBase Template for Todo App

Last updated 1 year ago