Problem with i18n urls where I'm redirected to the right url but the template is not found

Viewed 35

I read on the documentation page of i18n the following:

Warning

Ensure that you don’t have non-prefixed URL patterns that might collide with an automatically-dded language prefix.

I don't get this warning at all, what's the meaning of it??

I'm actually having a problem where I'm directed to my en/sitepages/news-events-listing or ar/sitepages/news-events-listing by a url defined like this in the root urls.py:

urlpatterns += i18n_patterns(
    path("sitepages/", include("sitepages.urls")),
)

and I've put it before thr wagtail urls line, but the template returned by the view inside sitepages/views.py:

def sitepages_view(request):
    template = loader.get_template('sitepages/news_events_page_listing.html')
    context = {
        'news_pages': NewsDetails.objects.all(),
        'events_pages': EventsDetails.objects.all(),
        'listing_page': NewsEventsListing.objects.first(),
    }
    return HttpResponse(template.render(context, request))

and the sitepages/urls.py:

app_name = "sitepages"

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

I can't think of anything but this warning, and that I might need to understand it to solve the problem, although I know that the order of urls in the root urls.py matter, so I might include it if anyone's think it might cause the problem but I don't want to make the question too long

Note: if I replace the HttpResponse with a simple one like this HttpResponse('Hi') it works!

1 Answers

If your simple HttpResponse('Hi') works but your Response with the rendered template is not, this has nothing to do with your i18n prefixed URLs but with the loading of the template.

Make sure the template actually exists in PROJECT_ROOT/sitepages/templates/sitepages/news_events_page_listing.html' and sitepages is listed in your INSTALLED_APPS.

As an alternative, you could move the template to any app you've listed in your INSTALLED_APPS and make sure the path, starting from the templates/ directory of that app, matches the template name you use in your view.

Related