ipegasus
ipegasus

Reputation: 15242

Django - How to get the language from the URL in a context processor?

I would like to display content in a template page according to the language in the URL.

And I have a context processor where I would like to capture the language from the URL. This is my code:

urls.py

url(r'^(?P<language>[a-z]{2})$', 'users.views.front_page_language'),

context_processors.py

def categories(request, language):
    return {'categories': category.objects.all(), 'request_language': language}

Currently 'request_language' returns 'None'. Is there a way to capture the language portion of the URL?

Eg: http://mydomain.com/en/ 'request_language' should return 'en'

Thanks in advance

SOLUTION

If I pass the language from the view to the template it works. Thanks everyone.

views.py

def front_page_language(request,language):
    return render_to_response('users/front_page.html', {'request_language': language}, context_instance=RequestContext(request))

Upvotes: 1

Views: 183

Answers (1)

alecxe
alecxe

Reputation: 473863

You are missing / at the end:

url(r'^(?P<language>[a-z]{2})/$', 'users.views.front_page_language'),

Upvotes: 1

Related Questions