2

I've got a menu to switch languages in my Django template. I call, via POST, the "/i18n/setlang/" view shown below to change the language setting. When I use a html form the user is redirected as expected, but when I use jQuery.post(), it is not.

What am I missing?

Thanks

Call to the view using jQuery.post():

$.post("/i18n/setlang/", { language: 'en', next: "{{request.path}}" });

The view is the following:

def set_language(request):
    """
    Redirect to a given url while setting the chosen language in the
    session or cookie. The url and the language code need to be
    specified in the request parameters.

    Since this view changes how the user will see the rest of the site, it must
    only be accessed as a POST request. If called as a GET request, it will
    redirect to the page in the request (the 'next' parameter) without changing
    any state.
    """
    next = request.REQUEST.get('next', None)

    if not next:
        next = request.META.get('HTTP_REFERER', None)
    if not next:
        next = '/'
    response = http.HttpResponseRedirect(next)
    if request.method == 'POST':
        lang_code = request.POST.get('language', None)
        if lang_code and check_for_language(lang_code):
            if hasattr(request, 'session'):
                request.session['django_language'] = lang_code
            else:
                response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)    
    return response
jul
  • 721
  • 1
  • 9
  • 18

4 Answers4

6

You don't need ajax. Just submit the form with javascript. You can hide the form.

<a href="#" onclick="setLang('tr');">
    <img src="/static/images/l2.png" alt="Türkçe" />
</a>
<a href="#" onclick="setLang('en');">
    <img src="/static/images/l1.png"  alt="English" />
</a>
<form id="langForm" action="/i18n/setlang/" method="post" style="display:none;">
    <select name="language">
        <option value="tr">Türkçe</option>
        <option value="en">English</option>
    </select>
</form>

...

<script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
    function setLang(lang){
        $('#langForm select').val(lang); 
        $('#langForm').submit();
        return false;
    }
</script>
hamdiakoguz
  • 15,795
  • 9
  • 33
  • 27
2

What are you expecting to happen? The AJAX post will be redirected as you expect, but the page itself won't. If you want the page itself to be redirected, why are you using AJAX?

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • What else can I use? Javascript? I don't want to use a form to have more freedom on the style. I kind of solved the issue by doing the redirection in javascript, as shown in my second post. – jul Jan 26 '10 at 16:50
  • 4
    @jul: How far is your "style freedom" restricted when using a form? Ajax makes no sense in your case as you are redirecting the user in any case, no need for this "overhead". – Felix Kling Jan 26 '10 at 16:55
0

Look at this.

It is the solution for your problem! ;)

Here it is a summary:

The situation: there is a view's method called by jQUery.ajax()

To redirect after Ajax, I've used the tips found here

But, I've had also further operations to do.

in the called view, call another method, like this:

[...]
if request.method == 'POST':
    if "obj" in request.POST: #ricevuti dati da ajax
        request.session['qual'] = request.POST['obj']
        return HttpResponseRedirect(reverse("xenopatients.views.qualReport"))

I save the data that I need in the session. I use this data in the called view.

Call another method it's the key for redirect the page of the browser. In fact, at the end of the called method, you can normally write&execute:

return render_to_response('measure/qualReport.html', {'name':name, 'list_report': report, 'message':'Measures correctly saved'}, RequestContext(request))
Community
  • 1
  • 1
Piero Alberto
  • 3,823
  • 6
  • 56
  • 108
0

I finally do the redirection in javascript:

$.post("/i18n/setlang/", { language: $(this).text(), next: "{{request.path}}" }, function(data) {
    top.location.href="{{request.path}}"; //redirection
});
jul
  • 721
  • 1
  • 9
  • 18