F
F
freiqqqq2020-11-28 11:39:55
Django
freiqqqq, 2020-11-28 11:39:55

Why Page not found (404)?

I don’t understand why 404 comes out, everything seems to be correct

upd: when you go to the second post,
MultipleObjectsReturned at /1928/card2/second_post/28/11/2020/
get() returned more than one Post -- it returned 2!
-------------------------------------

urls.py

urlpatterns = [
    path('', views.main_page,name='main_page'),
    path('<slug:card_slug>/', views.post_list, name='post_list'),
    path('<slug:card_slug>/<slug:post>/<int:day>/<int:month>/<int:year>/', views.post_detail, name='post_detail'),
    path('<slug:card_slug>/<int:post_id>/share/', views.post_share, name='post_share'),
    path('<slug:card_slug>/<slug:tag_slug>/',views.post_list, name='post_list_by_tag'),
    path('feed/', LatestPostsFeed(), name='post_feed'),
]
----------------------------------------------------------------------------------------------------------------
views.py

def post_detail(request, day, month, year, post,card_slug , tag_slug=None):
    post = get_object_or_404(Post,  slug=post,card__slug=card_slug, status='published',
                             publish__day=day,
                             publish__month=month,
                             publish__year=year)
    tag = None
    if tag_slug:
        tag = get_object_or_404(Tag, slug=tag_slug)
    comments = post.comments.filter(active=True)
    new_comment = None
    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            new_comment = comment_form.save(commit=False)
            new_comment.post = post
            new_comment.save()
            comment_form = CommentForm()
            return redirect(post)
    else:
        comment_form = CommentForm()
    return render(request, 'blog/post/detail.html', {'post':post,
                                                     'comments':comments,
                                                     'new_comment':new_comment,
                                                     'comment_form':comment_form,
                                                     'tag':tag})

-----------------------------------------
models.py 


    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.card.slug, 
                                                 self.slug,
                                                 self.publish.day,
                                                 self.publish.month,
                                                 self.publish.year,])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-11-28
@freiqqqq

Options:
1. url does not fall under your path
2. where get_object_or_404, the object is not found according to your criteria,
and MultipleObjectsReturned speaks for itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question