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:
self.request.user
: This assumes that you are inside a Django class-based view or a middleware whereself.request
is an instance of theHttpRequest
object, anduser
is the user associated with the request.context['tasks']
: This suggests that you are working with a context dictionary, commonly used in Django views to pass data to templates.context['tasks'].filter(...)
: It implies thatcontext['tasks']
is a queryset, likely a model queryset, on which you want to apply a filter..filter(user=self.request.user)
: This is filtering the queryset to include only those records where theuser
field is equal to theself.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.
Last updated