【发布时间】:2022-01-13 12:12:05
【问题描述】:
enter image description herei 正在创建一个简单的博客站点,我创建了 delete7edit 按钮,但是当我尝试重置应用程序并转到那里时,我得到了这个错误(标题)
这是我的代码view.py:
from django.views.generic import ListView
from . models import Article
from django.views.generic import ListView, DetailView, CreateView, UpdateView,
DeleteView
from .forms import PostForm, EditForm
from django.urls import reverse_lazy
class ArticleListView(ListView):
model = Article
template_name = 'home.html'
class ArticleDetailView(DetailView):
model = Article
template_name = 'detail.html'
class AddPostView(CreateView):
model = Article
form_class = PostForm
template_name = 'add_post.html'
#fields = '__all__'
#fields = ('title', 'body')
class UpdatePostView(UpdateView):
model = Article
form_class = EditForm
template_name = 'update_post.html'
#fields = ['title', 'title_tag', 'body']
class DeletePostView(DeleteView):
model = Article
template_name = 'delete_post.html'
success_url = reverse_lazy('home')
urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.ArticleListView.as_view(), name='home'),
path('article/<int:pk>', views.ArticleDetailView.as_view(), name='detail'),
path('add_post/', views.AddPostView.as_view(), name='add_post'),
path('article/edit/<int:pk>', views.UpdatePostView.as_view(), name='update_post'),
path('article/<int:pk>/remove', views.DeletePostView.as_view(), name='delete_post'),
]
模板 delete_post.html:
{% extends 'base.html' %}
{% block title %}Delete Blog Post{% endblock %}
{% block content %}
<h1>Delete Post...</h1>
<br/><br/>
<hr3>Delete: {{ post.title }}</hr3>
<br/>
<div class="mb-3">
<form method="POST">{% csrf_token %}
<strong>Are you sure?</strong><br/><br/>
<button class="btn btn-secondary">Delete Post!</button>
</div>
{% endblock %}
模板 update_post.html:
{% extends 'base.html' %}
{% block title %}Edit Blog Post{% endblock %}
{% block content %}
<h1>Update Post...</h1>
<br/><br/>
<div class="mb-3">
<form method="POST">{% csrf_token %}
{{ form.as_p }}
<button class="btn btn-secondary">Update</button>
</div>
{% endblock %}
请帮帮我,我是 django 的新手,我还不知道 ----------------------------- ------------------------------
【问题讨论】:
-
也分享您的模板
-
我有各种各样的模板,只发送那些删除和编辑?
-
是的,只分享相关的。
-
完成,我添加了它们
-
什么时候发生错误,点击“删除帖子!”正确的?还是在加载文章详细信息页面时?
标签: python django django-models django-views django-templates