Question

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) ?

Was it helpful?

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.

OTHER TIPS

in urls.py for Ajax requests move urls to "patterns"

urlpatterns = patterns('',
   # urls for Ajax etc
)
urlpatterns += i18n_patterns('',
    # sites urls
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top