Search code examples
pythonajaxdjango-cmsdjango-1.5django-i18n

Django 1.5 , language url redirect and javascript


I just migrated to django 1.5 and I am facing a problem with the new policy regarding url language redirects, my old Ajax POST to for example /search end up being redirected to /en/search with a GET of course. How to fix this (ideally without modifying too much code) ?


Solution

  • I came up with a solution, I named my ajax post urls like this:

    url(r"^search_engine/ajax_form/$", ajax_form, name='ajax-search')
    

    And in my template I did :

    <form method="post" action="{% url 'ajax-search' %}" id="search-form">
    

    And eventually in my script.js I did :

    var form = $('#search-form');
    
    $.ajax({
      type: 'POST',
      url: $(form).attr('action'),
      dataType: 'json',
      data : form.serialize(),
      success: function(data) {
        /*stuff*/
      }
    });
    

    If there is a better way to do this in django 1.5 I would like to know.

    EDIT: By the way this is painful when the URL has parameters.