Делаю мини-соц сеть на джанго. Делаю view на CBV , Создается пост, делаю возможность его редактивирования, форма для редактивирования отображается все гуд, после нажатия на кнопку сохранения даных, выдает ошибку 404 Page not found
Изменения в базе данных сохроняются, но пост пропадает и не открывается.
вот CBV - Страница всех постов, страница детального поста, создание поста, редактирование поста
class PostListView(PostFilterMixin, ListView):
model = Post
template_name = 'blog/index.html'
paginate_by = NUM_POST
def get_queryset(self):
return self.get_posts().annotate(comment_count=Count('comments')).order_by('-created_at')
class CategoryPostsView(PostFilterMixin, ListView):
model = Post
template_name = 'blog/category.html'
paginate_by = NUM_POST
def get_queryset(self):
category_slug = self.kwargs['category_slug']
category = get_object_or_404(Category.objects.filter(is_published=True), slug=category_slug)
return self.get_posts(category)
class PostDetailView(PostFilterMixin, DetailView):
model = Post
template_name = 'blog/detail.html'
context_object_name = 'post'
def form_valid(self, form):
if not self.test_func():
post_id = self.get_object().id
return redirect('blog:post_detail', post_id=post_id)
return super().form_valid(form)
def get_object(self, queryset=None):
post_id = self.kwargs['post_id']
post = get_object_or_404(self.get_posts(), id=post_id)
return post
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = CommentForm()
context['comments'] = Comment.objects.filter(post=self.object)
return context
def get_success_url(self):
return reverse('blog:post_detail', kwargs={'post_id': self.object.id})
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
form_class = PostCreateForm
template_name = 'blog/create.html'
success_url = reverse_lazy('blog:profile')
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def get_success_url(self):
return reverse('blog:profile', kwargs={'username': self.request.user.username})
class PostUpdateView(UserPassesTestMixin, UpdateView):
model = Post
form_class = PostCreateForm
template_name = 'blog/create.html'
def get_object(self, queryset=None):
post_id = self.kwargs['post_id']
post = get_object_or_404(Post, id=post_id)
return post
def test_func(self):
post = self.get_object()
return self.request.user.is_authenticated and post.author == self.request.user
def form_valid(self, form):
response = super().form_valid(form)
if not Post.objects.filter(id=self.object.id).exists():
return redirect('blog:post_detail', post_id=self.object.id)
return response
def get_success_url(self):
return reverse('blog:post_detail', kwargs={'post_id': self.object.id})
def get(self, request, *args, **kwargs):
if not self.test_func():
return redirect(reverse('blog:post_detail', kwargs={'post_id': self.kwargs['post_id']}))
return super().get(request, *args, **kwargs)
мой URLS адреса
urlpatterns = [
path('', views.PostListView.as_view(), name='index'),
path(
'profile/<slug:username>/', views.ProfileView.as_view(),
name='profile'),
path(
'profile/<slug:username>/edit/', views.ProfileUpdateView.as_view(),
name='edit_profile'),
path(
'posts/<int:post_id>/', views.PostDetailView.as_view(),
name='post_detail'),
path('posts/create/', views.PostCreateView.as_view(), name='create_post'),
path(
'posts/<int:post_id>/edit/', views.PostUpdateView.as_view(),
name='edit_post'),