Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page switching using AJAX in Django

I am trying to create site navigation using AJAX. I have navigation menu with links to different views (using {% url name %} in template). The thing I am trying to do is to load the page content using AJAX. The page content I am trying to load is enclosed in content block ({% block content %}).

I also found this snippet http://djangosnippets.org/snippets/942/, but I want to use the views I have already defined and only get the content using ajax.

Any suggestions?

like image 385
Viktor Stískala Avatar asked Jul 01 '11 10:07

Viktor Stískala


People also ask

Can you use AJAX with Django?

Using Ajax in Django can be done by directly using an Ajax library like JQuery or others. Let's say you want to use JQuery, then you need to download and serve the library on your server through Apache or others. Then use it in your template, just like you might do while developing any Ajax-based application.

How send data from AJAX to Django?

To send and receive data to and from a web server, AJAX uses the following steps: Create an XMLHttpRequest object. Use the XMLHttpRequest object to exchange data asynchronously between the client and the server. Use JavaScript and the DOM to process the data.

How do I post with JQuery AJAX in Django?

On submitting the form, serialize the form data and create an AJAX POST request, then send it to the server. On successful request, append the row to the table. Note we have used the revered URL , which is discussed in the urls.py section. This helps you not to write the URL path in a hardcoded way.

How does AJAX save data in Django?

Create the AJAX View POST to the form—this is known as binding a form to a dataset. Next, we check the form to see if the submitted data is valid. Django validates the data behind the scenes for us. If the data is correct, we save the form to the database with form.


2 Answers

You should use django-pjax which is built exactly for that kind of thing.

All you will have to do is to, in the base template, include the whole page or just the block content based on whether the request is ajax or not.

django-pjax does the AJAX calls using the jQuery and manipulates history using HTML5 push state API, which is a very good way to do it and also gracefully degrades in IE older versions.

like image 182
lprsd Avatar answered Sep 28 '22 13:09

lprsd


Template tags like {% block content %} are long gone by the time AJAX sees things. What you want to do is create a named <div> in your content block, like:

{% block content %}
<div id="content"></div>
{% endblock content %}

Then you can use something like this (jQuery) code to load the <div> when needed:

$("#content").load(url, data, loadComplete);

where url is the URL you want to load (HTML expected in return), data is the form data (if any; can be omitted), and loadComplete is the optional function to be called when the data is loaded, and is of the form function loadComplete(responseText, textStatus, XMLHttpRequest) {...}. Even if you don't want to use jQuery, you can get the non-minified jQuery source and see how they do it.

like image 32
Mike DeSimone Avatar answered Sep 28 '22 13:09

Mike DeSimone