O ToDoList é um desafio muito comum solicitado por empresas, com objetivo de verificar a capacitação de possíveis candidatos à vagas em atuação na área de desenvolvimento de sistemas. O sistema ToDoList é, basicamente, um programa onde tem-se uma lista de tarefas, das quais pode-se marcar como 'completa' ou 'incompleta', além das operações tradicionais de CRUD.
tasks/templates/tasks/delete.html:
<p>Excluir a task "{{item}}"?</p>
<a href="{% url 'list' %}">Cancelar</a>
<form method="POST">
{% csrf_token %}
<input type="submit">
</form>
tasks/templates/tasks/list.html:
<h3>To do Django UB Social</h3>
<form method="POST">
{% csrf_token %}
{{form}}
<input type="submit">
</form>
{% for task in tasks %}
<div>
<a href="{% url 'update' task.id %}">Update</a>
<a href="{% url 'detail' task.id %}">Detail</a>
<a href="{% url 'delete' task.id %}">Delete</a>
{% if task.complete == True %}
<s>{{task}}</s>
{% else %}
<span>{{task}}</span>
{% endif %}
</div>
{% endfor %}
tasks/templates/tasks/detail.html:
<a href="{% url 'list' %}">Voltar</a><br>
<div class="main">
{{task.id}}<br>
{% if task.complete == True %}
<s>{{task.title}}</s>
{% else %}
<span>{{task.title}}</span>
{% endif %}
</div>
tasks/templates/tasks/update.html:
<h3>Update Task</h3>
<form method="POST">
{% csrf_token %}
{{form}}
<input type="submit">
</form>
admin.py:
from django.contrib import admin
from .models import *
admin.site.register(Task)
apps.py:
from django.apps import AppConfig
class TasksConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'tasks'
forms.py:
from django import forms
from django.forms import ModelForm
from .models import *
class TaskForm(forms.ModelForm):
class Meta:
model = Task
fields = '__all__'
models.py:
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=200)
complete = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name="list"),
path('task/<str:pk>/', views.detailTask, name="detail"),
path('update/<str:pk>/', views.updateTask, name="update"),
path('delete/<str:pk>/', views.deleteTask, name="delete"),
]
views.py:
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import *
from .forms import *
def index(request):
tasks = Task.objects.all()
form = TaskForm()
if request.method == 'POST':
form = TaskForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
context = {'tasks':tasks, 'form':form}
return render(request, 'tasks/list.html', context)
def detailTask(request, pk):
context = {}
context["task"] = Task.objects.get(id=pk)
return render(request, 'tasks/detail.html', context)
def updateTask(request, pk):
task = Task.objects.get(id=pk)
form = TaskForm(instance=task)
if request.method == 'POST':
form = TaskForm(request.POST, instance=task)
if form.is_valid():
form.save()
return redirect('/')
context = {'form':form}
return render(request, 'tasks/update.html', context)
def deleteTask(request, pk):
item = Task.objects.get(id=pk)
if request.method == 'POST':
item.delete()
return redirect('/')
context = {'item':item}
return render(request, 'tasks/delete.html', context)
todo/settings.py:
Modificar, no arquivo original, com o trecho de código abaixo
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tasks',
]
todo/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('tasks.urls'))
]
Elaborado por Mateus Schwede
ubsocial.github.io