我已经在post_details.html中添加了编辑和删除按钮。这是编辑或删除由帖子所有者添加的帖子。不幸的是,我在访问post_details.html页面时总是遇到错误
错误是:
NoReverseMatch at/2020/10/31/django框架 找不到参数为“(14,)”的“后期更新”的反向项。尝试了1个模式:['(?P[0-9]+)/(?P[0-9]+)/(?P[0-9]+)/(?P[-a-zA-Z0-9]+)/(?P[0-9]+)/更新$'][/strong>
求你了,我需要你的帮助
以下是代码片段:
型号.py
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset(). \
filter(status='published')
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250,
unique_for_date='publish')
author = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='blog_posts')
header_image = models.ImageField(null=True, blank=True, upload_to='post')
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices=STATUS_CHOICES, default='published')
objects = models.Manager() # Default manager
published = PublishedManager() # Custom manager
tags = TaggableManager()
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail',
args=[self.publish.year,
self.publish.month,
self.publish.day,
self.slug])
def save(self, *args, **kwargs): # new
if not self.slug:
self.slug = slugify(self.title)
return super().save(*args, **kwargs)
视图.py
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Post
fields = ['title', 'body', 'tags', 'status']
template_name = 'blog/post/post_form.html'
def form_valid(self, form):
form.instance.author = self.request.user
return super(PostUpdateView, self).form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
model = Post
template_name = 'blog/post/post_confirm_delete.html'
success_url = '/'
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
url.py
from django.urls import path
from . import views
from .views import PostCreateView, PostUpdateView, PostDeleteView
app_name = 'blog'
urlpatterns = [
# Post views
path('', views.post_list, name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>', views.post_detail, name='post_detail'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/<int:id>/update', PostUpdateView.as_view(), name='post_update'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/<int:id>/delete', PostDeleteView.as_view(), name='post_delete'),
post_detail.html
{% if post.author == user %}
<div>
<a class="btn btn-secondary btn-sm mt-1 mb-3"
href="{% url 'blog:post_update' post.id %}">Edit</a>
<a class="btn btn-danger btn-sm mt-1 mb-3"
href="{% url 'blog:post_delete' post.id %}">Delete</a>
</div>
{% endif %}
主要项目url
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls', namespace='blog')),
]
已更正的代码
传递适当的值,如年、月、日、slug。。。。或者在您的url中删除它…
相关问题 更多 >
编程相关推荐