todo app and categorys chatGPT4
Q: As a python django doveloper, create todo app there is display the categorys officee work and home work
Ans: Sure, here's an example of how you can create a simple todo app using Django and display categories "Office work" and "Home work":
- First, create a new Django project:
- First, create a new Django project::
- Next, create a new Django app within the project:
- Now, open the `tasks/models.py` file and define a `Task` model with a `category` field:
- Next, create a migration for the `Task` model and apply it to the database:
- Now, open the `tasks/views.py` file and define a `TaskListView` class-based view that displays all tasks grouped by category:
- Create a new template file at `tasks/task_list.html` and use it to display the tasks grouped by category:
- Finally, add a URL pattern to the `tasks/urls.py` file that maps the `TaskListView` to the root URL:
django-admin startproject todoapp
django-admin startproject todoapp
cd todoapp python manage.py startapp tasks
from django.db import models class Task(models.Model): title = models.CharField(max_length=100) description = models.TextField() category = models.CharField(max_length=20, choices=(('office', 'Office work'), ('home', 'Home work'))) completed = models.BooleanField(default=False) def __str__(self): return self.title
python manage.py makemigrations tasks python manage.py migrate
from django.views.generic.list import ListView from .models import Task class TaskListView(ListView): model = Task def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['office_tasks'] = Task.objects.filter(category='office') context['home_tasks'] = Task.objects.filter(category='home') return context
{% extends 'base.html' %} {% block content %} <h2>Office work</h2> <ul> {% for task in office_tasks %} <li>{{ task.title }}</li> {% empty %} <li>No tasks</li> {% endfor %} </ul> <h2>Home work</h2> <ul> {% for task in home_tasks %} <li>{{ task.title }}</li> {% empty %} <li>No tasks</li> {% endfor %} </ul> {% endblock %}
from django.urls import path from .views import TaskListView urlpatterns = [ path('', TaskListView.as_view(), name='task_list'), ]
That's it! You can now run the Django development server with `python manage.py runserver` and visit `http://localhost:8000/` to see the list of tasks grouped by category.
No comments