- Django 3 Web Development Cookbook
- Aidas Bendoraitis Jake Kronika
- 427字
- 2025-04-04 13:15:08
How to do it...
The CRUDL functionality in Django consists of forms, views, and URL rules. Let's create them:
- Add a new forms.py file to the ideas app with the model form for adding and changing the instances of your Idea model:
# myprojects/apps/ideas/forms.py
from django import forms
from .models import Idea
class IdeaForm(forms.ModelForm):
class Meta:
model = Idea
fields = "__all__"
- Add a new views.py file to the ideas app with the views to manipulate the Idea model:
# myproject/apps/ideas/views.py
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import ListView, DetailView
from .forms import IdeaForm
from .models import Idea
class IdeaList(ListView):
model = Idea
class IdeaDetail(DetailView):
model = Idea
context_object_name = "idea"
@login_required
def add_or_change_idea(request, pk=None):
idea = None
if pk:
idea = get_object_or_404(Idea, pk=pk)
if request.method == "POST":
form = IdeaForm(
data=request.POST,
files=request.FILES,
instance=idea
)
if form.is_valid():
idea = form.save()
return redirect("ideas:idea_detail", pk=idea.pk)
else:
form = IdeaForm(instance=idea)
context = {"idea": idea, "form": form}
return render(request, "ideas/idea_form.html", context)
@login_required
def delete_idea(request, pk):
idea = get_object_or_404(Idea, pk=pk)
if request.method == "POST":
idea.delete()
return redirect("ideas:idea_list")
context = {"idea": idea}
return render(request, "ideas/idea_deleting_confirmation.html", context)
- Create the urls.py file in the ideas app with the URL rules:
# myproject/apps/ideas/urls.py
from django.urls import path
from .views import (
IdeaList,
IdeaDetail,
add_or_change_idea,
delete_idea,
)
urlpatterns = [
path("", IdeaList.as_view(), name="idea_list"),
path("add/", add_or_change_idea, name="add_idea"),
path("<uuid:pk>/", IdeaDetail.as_view(), name="idea_detail"),
path("<uuid:pk>/change/", add_or_change_idea,
name="change_idea"),
path("<uuid:pk>/delete/", delete_idea, name="delete_idea"),
]
- Now, let's plug in these URL rules to the project's URL configuration. We will also include the accounts' URL rules from the Django-contributed auth app, so that our @login_required decorator works properly:
# myproject/urls.py
from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
from django.shortcuts import redirect
urlpatterns = i18n_patterns(
path("", lambda request: redirect("ideas:idea_list")),
path("admin/", admin.site.urls),
path("accounts/", include("django.contrib.auth.urls")),
path("ideas/", include(("myproject.apps.ideas.urls", "ideas"),
namespace="ideas")),
)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static("/media/", document_root=settings.MEDIA_ROOT)
- You should now be able to create the following templates:
- registration/login.html with a form to log in
- ideas/idea_list.html with a list of ideas
- ideas/idea_detail.html with the details about an idea
- ideas/idea_form.html with a form to add or change an idea
- ideas/idea_deleting_confirmation.html with an empty form to confirm idea deletion
In the templates, you can address the URLs of the ideas app via the namespace and path names as follows:
{% load i18n %}
<a href="{% url 'ideas:change_idea' pk=idea.pk %}">{% trans "Change this idea" %}</a>
<a href="{% url 'ideas:add_idea' %}">{% trans "Add idea" %}</a>
If you get stuck or want to save time, check the corresponding templates in the code files for this book, which you can find at https://github.com/PacktPublishing/Django-3-Web-Development-Cookbook-Fourth-Edition/tree/master/ch03/myproject_virtualenv/src/django-myproject/myproject/templates/ideas.