setlang using AJAX

331 views
Skip to first unread message

Luca Casagrande

unread,
May 30, 2011, 7:07:11 AM5/30/11
to Django users
Hello everybody,
I'd like to use an AJAX request without any form to change the
language of my site.
My problem is that I haven't found a way to avoid the csrf_token
error..
How can I generate the token without any form?

The other way it to use setlang with a GET request but, according to
docs, this seems no more possible.

Thanks for your support.
Luca

urukay

unread,
May 30, 2011, 9:30:38 AM5/30/11
to Django users
hi,

but you have to reload the whole page anyway or you want only part of
the page to be translated?
There's a way to change language without form.

R.

On 30. Máj, 13:07 h., Luca Casagrande <luca.casagra...@gmail.com>
wrote:

Luca Casagrande

unread,
May 30, 2011, 10:19:45 AM5/30/11
to Django users
Hi Radovan,
this code should go on a web page before the real application.
Basically I'd like to have the user click on a flag representing the
language and load the real site with the appropriate language.

Thanks
L.

urukay

unread,
May 30, 2011, 10:42:55 AM5/30/11
to Django users
Hi Luca,

this is what i had on my site:

<ul class="language-bar">
{% ifequal LANGUAGE_CODE 'sk' %}
<li><span class="lb-sk">{% trans 'Slovak' %}</span></li>
<li><a class="lb-en" href="#en" title="{% trans 'English' %}">{%
trans 'English' %}</a></li>
{% else %}
<li><a class="lb-sk" href="#sk" title="{% trans 'Slovak' %}">{%
trans 'Slovak' %}</a></li>
<li><span class="lb-en">{% trans 'English' %}</span></li>
{% endifequal %}
</ul>
<script type="text/javascript">
$('.language-bar a').click(function(){
$.post('/i18n/setlang/', {language:$(this).attr('href').split('#')
[1], next:"{{request.path}}" },function(data)
{top.location.href="{{request.path}}";});
return false;
});
</script>

Maybe it is not the best solution, but it's much nicer than
selectboxes. It reloads the whole page,but it's not a problem adjust
it to your needs.

Radovan

On 30. Máj, 16:19 h., Luca Casagrande <luca.casagra...@gmail.com>

Luca Casagrande

unread,
May 30, 2011, 11:03:46 AM5/30/11
to Django users
Thank you very much, for your help and your code.
The problem is that the POST request is missing the csrf_token and so
I have got a 403 Error.
Have you got the same problem?

Thx
Luca

urukay

unread,
May 30, 2011, 11:16:33 AM5/30/11
to Django users
no, i'm using Django 1.2.1.and i can't see it but i have faced with
this problem before.
Got all csrf middlewares in your Settings.py?

On 30. Máj, 17:03 h., Luca Casagrande <luca.casagra...@gmail.com>

Kirill Spitsin

unread,
May 30, 2011, 11:22:31 AM5/30/11
to django...@googlegroups.com
On Mon, May 30, 2011 at 08:03:46AM -0700, Luca Casagrande wrote:
> Thank you very much, for your help and your code.
> The problem is that the POST request is missing the csrf_token and so
> I have got a 403 Error.

https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#ajax

--
Kirill Spitsin

Tiago Barufi

unread,
Feb 12, 2015, 1:05:10 PM2/12/15
to django...@googlegroups.com
You could simply add 

csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,

to the parameters on $.post. That would surely prevent the 403.
Reply all
Reply to author
Forward
0 new messages