Site icon JnPnote

Following Django Tutorial (Todo App)-Part4

This post is for myself to remember how to start the basic Django project.
I did follow the tutorial from the JustDjango Learn website free tutorial.
And this is the first tutorial named Django Crash Course.
As previous parts, I am not going over all the details and descriptions for each part. There are good explanations on video of the JustDjango Learn. So, visit their site and try their tutorials if you need more details. Also, the orders of this post and their video might be different because I put things first what I think should come first.

In this post, I will cover about the CRUD(Create, Retrieve, Update, Delete and List)

1. Detail(Retrieve)

def todo_detail(request, id):
	todo = Todo.objects.get(id=id)
	context = {
		"todo": todo
	}
	return render(request, "todo_detail.html", context)
from django.urls import path

from .views import todo_detail, todo_list

app_name = 'todos'

urlpatterns = [
	path('', todo_list),
	path('<id>/', todo_detail),
]
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Django-Todo</title>
</head>
<body>
	{% if request.user.is_authenticated %}
		Hello {{ request.user.username }}
	{% else %}
		Hello anonymous user
	{% endif %}
	The todo name is: {{ todo.name }}
	The due date is {{ todo.due_date }}
</body>
</html>
<a href="/{{ todo.id }}">{{ todo.name }}</a>

2. Create

from django import forms
from .models import Todo


class TodoForm(forms.ModelForm):
	class Meta:
		model = Todo
		fields = ['name', 'due_date']
from .forms import TodoForm



def todo_create(request):
	form = TodoForm(request.POST or None)
	if form.is_valid():
		form.save()
	context = {"form": form}
	return render(request, "todo_create.html", context)
	
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Django-Todo</title>
</head>
<body>
	<h4>Create a todo</h4>
	<form method="POST">
		{% csrf_token %}
		{{ form.as_p }}
		<button type='submit'>Submit</button>
	</form>
</body>
</html>
<a href="/create">Create a todo</a>
from django.urls import path

from .views import todo_detail, todo_list, todo_create

app_name = 'todos'

urlpatterns = [
	path('', todo_list),
	path('<id>/', todo_detail),
	path('create/', todo_create)
]
return redirect('/')

3. Update

def todo_update(request, id):
	todo = Todo.objects.get(id=id)
	form = TodoForm(request.POST or None, instance=todo)
	if form.is_valid():
		form.save()
		return redirect('/')
	context = {"form": form}
	return render(request, "todo_update.html", context)
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Django-Todo</title>
</head>
<body>
	<h4>Update todo</h4>
	<form method="POST">
		{% csrf_token %}
		{{ form.as_p }}
		<button type='submit'>Submit</button>
	</form>
</body>
</html>
<a href="/{{ todo.id}}/update">Update this todo</a>

4. Delete

def todo_delete(request, id):
	todo = Todo.objects.get(id=id)
	todo.delete()
	return redirect("/")
<a href="/{{ todo.id}}/delete">Delete this todo</a>

Done for this post and next will be about the styling.

Exit mobile version