New files
templates/app/article_update.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create article</title>
</head>
<body>
<h1>Edit Article</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save</button>
</form>
</body>
</html>
templates/app/article_delete.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create article</title>
</head>
<body>
<h1>Delete Article</h1>
<p>Are you sure you want to delete "{{ article.title }}"?</p>
<form method="POST">
{% csrf_token %}
<button type="submit">Delete</button>
</form>
</body>
</html>
Modified files
app/urls.py
---
+++
@@ -1,7 +1,14 @@
from django.urls import path
-from app.views import home, ArticleCreateView
+from app.views import (
+ ArticleListView,
+ ArticleCreateView,
+ ArticleUpdateView,
+ ArticleDeleteView,
+)
urlpatterns = [
- path("", home, name="home"),
- path("articles/create/", ArticleCreateView.as_view(), name="create_article")
+ path("", ArticleListView.as_view(), name="home"),
+ path("articles/create/", ArticleCreateView.as_view(), name="create_article"),
+ path("articles/<int:pk>/update/", ArticleUpdateView.as_view(), name="update_article"),
+ path("articles/<int:pk>/delete/", ArticleDeleteView.as_view(), name="delete_article"),
]
app/views.py
---
+++
@@ -1,17 +1,38 @@
from django.shortcuts import render
from django.urls import reverse_lazy
-from django.views.generic import CreateView
+from django.views.generic import (
+ CreateView,
+ ListView,
+ UpdateView,
+ DeleteView
+)
from app.models import Article
-def home(request):
- articles = Article.objects.all()
- return render(request, "app/home.html", {"articles": articles})
+class ArticleListView(ListView):
+ template_name = "app/home.html"
+ model = Article
+ context_object_name = "articles"
class ArticleCreateView(CreateView):
template_name = "app/article_create.html"
model = Article
fields = ["title", "status", "content", "word_count", "twitter_post"]
- success_url = reverse_lazy("home")+ success_url = reverse_lazy("home")
+
+
+class ArticleUpdateView(UpdateView):
+ template_name = "app/article_update.html"
+ model = Article
+ fields = ["title", "status", "content", "word_count", "twitter_post"]
+ success_url = reverse_lazy("home")
+ context_object_name = "article"
+
+
+class ArticleDeleteView(DeleteView):
+ template_name = "app/article_delete.html"
+ model = Article
+ success_url = reverse_lazy("home")
+ context_object_name = "article"