最近我一直在为我的课程做一个董事会项目。我为某个模型写了一个deleteview。它工作了一段时间,然后它开始给我404错误。下面我将附上我的代码和整个视图和URL。
ps:我知道我不应该给出所有这些,但是谁知道呢,也许你可以帮我解决其他的错误
views.py
class Post_list(LoginRequiredMixin, ListView):
model = Post
template_name = 'Post/list_of_posts.html'
context_object_name = 'Posts'
def get_context_data(self, *args,**kwargs):
cat_menu = Category.objects.all()
context = super(Post_list, self).get_context_data(*args,**kwargs)
context["cat_menu"] = cat_menu
return context
class Post_details(LoginRequiredMixin, DetailView):
model = Post
template_name = 'Post/post_details.html'
context_object_name = 'Post'
class Post_create(LoginRequiredMixin, CreateView):
model = Post
template_name = 'Post/post_create.html'
form_class = PostForm
class Post_update(LoginRequiredMixin, UpdateView):
template_name = 'Post/post_update.html'
form_class = EditForm
context_object_name = 'Post'
def get_object(self,**kwargs):
# Here we are getting the id so Django could stalk the change
ID = self.kwargs.get('pk')
return Post.objects.get(pk=ID)
class Post_delete(LoginRequiredMixin, DeleteView):
template_name = 'Post/post_delete.html'
queryset = Post.objects.all()
context_object_name = 'Post'
success_url = '/Posts'
def CategoryView(request, cats):
category_posts = Post.objects.filter(cat=1)
return render(request, 'Post/categories.html', {'cats': cats,
'category_posts': category_posts})
class Add_comment(LoginRequiredMixin, CreateView):
model = Comments
template_name = 'Post/add_comment.html'
form_class = CommentForm
success_url = '/Posts'
def form_valid(self, form):
form.instance.Post_id = self.kwargs['pk']
return super().form_valid(form)
class CommentEdit(LoginRequiredMixin, UpdateView):
template_name = 'Post/edit_comment.html'
form_class = CommentEditForm
context_object_name = 'comments'
def get_object(self,**kwargs):
# Here we are getting the id so Django could stalk the change
ID = self.kwargs.get('pk')
return Post.objects.get(pk=ID)
class CommentDelete(LoginRequiredMixin, DeleteView):
model = Comments
template_name = 'Post/comment_delete.html'
queryset = Comments.objects.all()
context_object_name = 'comments'
success_url = '/Posts'
url.py
urlpatterns = [
path('', Post_list.as_view()),
path('<int:pk>', Post_details.as_view(), name='post_details'),
path('add/', Post_create.as_view(), name='post_create'),
path('<int:pk>/edit', Post_update.as_view(), name='post_update'),
path('<int:pk>/delete', Post_delete.as_view(), name='post_delete'),
path('Category/<str:cats>/', CategoryView, name='category'),
path('<int:pk>/comment/', Add_comment.as_view(), name='add_comment'),
path('<int:pk>/comment/edit', CommentEdit.as_view(), name='edit_comment'),
path('<int:pk>/deletecomment', CommentDelete.as_view(), name='delete_comment'),
]
暂无答案!
目前还没有任何答案,快来回答吧!