I am sure this has a trivially easy answer but I have spent a couple of hours with Google and Stack Overflow without finding any answer.
I am building a Django CMS for a multi-language web site. URLS take the form:
https://example.com/xx/subject
where x is a two-letter code for a language (en=English, fr=French). For example:
https://example.com/en/about-us
https://example.com/fr/a-propos
I need to be able to access the requested URL from within the handler404 view to provide appropriate content to the visitor.
I currently have a working 404 redirect in the Django app:
project.urls.py
handler404 = 'myapp.views.error404'
myapp.views.py
def PageView(request, lang, subject):
[creates a page with DB content based on lang & subject]
def error404(request, *args, **kwargs):
return PageView(request,'en','404page',)
I would like to know what the requested URL was in order to redirect the visitor to a page in their language.
Under this system the 404 page will be a page in the CMS like any other, so it will be editable by the CMS user (I am not using Django templates to create pages).
There will be one "404page" entry in the DB for each language.
Here is a non-working example of the kind of thing I'm looking for:
def error404(request, *args, **kwargs):
lang = args[0]
subject = args[1]
if lang = 'fr':
return PageView(request,'fr','error404',)
else:
return PageView(request,'en','error404',)
If someone requests example.com/fr/quelque-chose, I want to show them content from fr/404Page.
If someone requests example.com/mlkqdjsf or example.com/en/not-a-page, I want to show them content from en/404page.
In both cases I will be using the PageView view that I use to construct the regular site pages.
I think you can do it with request.path
:
def error404(request, *args, **kwargs):
lang = request.path.split('/')[1]
if lang = 'fr':
return PageView(request,'fr','error404',)
else:
return PageView(request,'en','error404',)