如何在Django中修复此未找到的URL

时间:2019-06-02 11:39:03

标签: python django

所以我有这个Web应用程序,它有一个主模型Post和一个子模型Comment。当我想批准评论(使用我的超级用户)对Post时,我正在写评论,然后将其发送给批准。当超级用户单击批准!事情出了问题,我被重定向到404。我在代码中找不到问题。

“未找到:/ comment / 9 / approve /”

曾尝试弄乱urls.py和views.py,但它的重要性似乎对我来说是个问题。

在urls.py中:

url(r'^post/(?P<pk>\d+)/comment/$',views.add_comment_to_post,name 
     ='add_comment_to_post'),
url(r'^post/(?P<pk>\d+)$',views.PostDetailView.as_view(),name='post_detail'),

在views.py中:

@login_required
def comment_approve(request,pk):
comment = get_object_or_404(Comment,pk=pk)
comment.approve()
return redirect('post_detail',pk=comment.post.pk)

models.py中的注释模型:

class Comment(models.Model):
post = models.ForeignKey('Myblog.post',related_name='comments',on_delete=models.CASCADE)
author = models.CharField(max_length=200)
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
approve_comment = models.BooleanField(default=False)

def approve(self):
    self.approved_comment = True
    self.save()

forms.py中的注释表单:

 class CommentForm(forms.ModelForm):
class Meta:
    model = Comment
    fields = ('author','text')

widgets={
    'author':forms.TextInput(attrs={'class':'textinputclass'}),
    'text':forms.Textarea(attrs={'class':'editable medium-editor-textarea'})
}

在urls.py中:

urlpatterns =[
url(r'^$',views.PostListView.as_view(),name='post_list'),
url(r'^about/$',views.AboutView.as_view(),name='about'),
url(r'^post/(?P<pk>\d+)$',views.PostDetailView.as_view(),name='post_detail'),
url(r'^post/new/$',views.CreatePostView.as_view(),name='post_new'),
url(r'^post/(?P<pk>\d+)/edit/$',views.PostUpdateView.as_view(),name='post_edit'),
url(r'^post/(?P<pk>\d+)/remove/$',views.PostDeleteView.as_view(),name='post_remove'),
url(r'^drafts/$',views.DraftListView.as_view(),name='post_draft_list'),
url(r'^post/(?P<pk>\d+)/comment/$',views.add_comment_to_post,name ='add_comment_to_post'),
url(r'^comment/(?P<pk>\d+)/approve/$',views.comment_approve,name='comment_approve'),
url(r'^comment/(?P<pk>\d+)/remove/$',views.comment_remove,name='comment_remove'),
url(r'^post/(?P<pk>\d+)/publish/$',views.post_publish,name='post_publish'),

]

我希望评论获得批准后会重定向回帖子详细信息视图

0 个答案:

没有答案