V
V
Vlad2021-02-18 23:28:21
Django
Vlad, 2021-02-18 23:28:21

Why does a 404 error occur when going to a page with a form?

Good afternoon!

I can't figure out why when clicking on a link to a page with a form, a 404 error pops up with the following content:

Request Method:	GET
Request URL:	http://localhost:8000/news/add-news/
Raised by:	news.views.ViewNews


For some reason, Django points to the ViewNews view, although I have a completely different view responsible for the form: CreateNews.

In order:
1. Link to the page with the form:
<li class="nav-item"><a class="nav-link" href="{%url 'add_news'%}">Добавить новость</a></li>


2. Path from url.py for this link:
path('news/add-news/', CreateNews.as_view(), name='add_news')


3. CreateNews view:
class CreateNews(CreateView): 
    form_class = NewsForm
    template_name = 'news/add_news.html'


Just in case, I’ll write the get_absolute_url method for the News model here, because as I understand it, it affects something in this case:
def get_absolute_url(self):
        return reverse('view_news', kwargs={'slug': self.slug})


and the ViewNews method, since Django refers to it:
class ViewNews(DetailView):  
    model = News
    context_object_name = 'news_item'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['unwanted_link'] = News.objects.get(slug=self.kwargs['slug']).category.pk
        return context


Many thanks in advance for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-02-18
@Vlad1987

Because ViewNews used to intercept /news/add-news/ , treating add-news as a slug.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question