Miriam Arbaji
Miriam Arbaji

Reputation: 329

Page not found for the template returned by TemplateResponse in Django

I have an app which is called sitepages which have three models I wanna pass their objects to a html page called listing.html which is inside templates/sitepages the sitepages urls.py contains:

from django.views.generic import TemplateView
from django.urls import include
from django.urls import path
from sitepages import views

app_name = "sitepages"

urlpatterns = [
    path("news-events-listing/", views.view, name='listing'),
]

the sitepages/views.py:

from .models import Listing, Details1, Details2
from django.template.response import TemplateResponse

def view(request):
    return TemplateResponse(request, 'sitepages/listing.html', {
            'first_pages': Details1.objects.all(),
            'seconde_pages': Details1.objects.all(),
            'listing_page': Listing.objects.first(),
        })

in the root urls.py I added:

path("", include("sitepages.urls", namespace='sitepages'))

when I put in any template the following:

<a href="{% url 'sitepages:listing' %}">Listing</a>

it redirects me to the url /news-events-listing but no page is found and it gives me 404 error...what am I doing wrong? why the template is not returned? I should mention that I'm using wagtail for the whole site (I don't know if it's related)

Upvotes: 0

Views: 461

Answers (1)

gasman
gasman

Reputation: 25227

In your root urls.py, make sure your new line

path("", include("sitepages.urls", namespace='sitepages'))

appears before the path("", include(wagtail_urls)) line. The wagtail_urls pattern matches any URL path and passes it to Wagtail to be handled as a Wagtail page, so any patterns after it will never be reached.

Upvotes: 1

Related Questions