I am new to Django and pretty new to Ajax. I am working on a project where I need to integrate the two. I believe that I understand the principles behind them both, but have not found a good explanation of the two together.
Could someone give me a quick explanation of how the codebase must change with the two of them integrating together?
For example, can I still use the HttpResponse
with Ajax, or do my responses have to change with the use of Ajax? If so, could you please provide an example of how the responses to the requests must change? If it makes any difference, the data I am returning is JSON.
Simple and Nice. You don't have to change your views. Bjax handles all your links. Check this out: Bjax
Usage:
Finally, include this in the HEAD of your html:
For more settings, checkout demo here: Bjax Demo
I have tried to use AjaxableResponseMixin in my project, but had ended up with the following error message:
That is because the CreateView will return a redirect response instead of returning a HttpResponse when you to send JSON request to the browser. So I have made some changes to the
AjaxableResponseMixin
. If the request is an ajax request, it will not call thesuper.form_valid
method, just call theform.save()
directly.Further from yuvi's excellent answer, I would like to add a small specific example on how to deal with this within Django (beyond any js that will be used). The example uses
AjaxableResponseMixin
and assumes an Author model.Source: Django documentation, Form handling with class-based views
The link to version 1.6 of Django is no longer available updated to version 1.11
Even though this isn't entirely in the SO spirit, I love this question, because I had the same trouble when I started so I'll give you a quick guide. Obviously you don't understand the principles behind them (don't take it as an offense, but if you did you wouldn't be asking).
Django is server-side. It means, say a client goes to url you have a function inside views that renders what he sees and returns a response in html. let's break it up into examples:
views.py
index.html:
urls.py
That's an example of the simplest of usages. Going to
127.0.0.1:8000/hello
means a request to the hello function, going to127.0.0.1:8000/home
will return theindex.html
and replace all the variables as asked (you probably know all this by now).Now let's talk about AJAX. AJAX calls are client-side code that does asynchronous requests. That sounds complicated, but it simply means it does a request for you in the background and then handles the response. So when you do an AJAX call for some url, you get the same data you would get as a user going to that place.
For example, an ajax call to
127.0.0.1:8000/hello
will return the same thing it would as if you visited it. Only this time, you have it inside a js function and you can deal with it however you'd like. Let's look at a simple use case:The general process is this:
127.0.0.1:8000/hello
as if you opened a new tab and did it yourself.Now what would happen here? You would get an alert with 'hello world' in it. What happens if you do an ajax call to home? Same thing, you'll get an alert stating
<h1>Hello world, welcome to my awesome site</h1>
.In other words - there's nothing new about AJAX calls. They are just a way for you to let the user get data and information without leaving the page, and it makes for a smooth and very neat design of your website. A few guidelines you should take note of:
console.log
things to debug. I won't explain in detail, just google around and find out about it. It would be very helpful to you.csrf_token
. With AJAX calls, a lot of times you'd like to send data without refreshing the page. You'll probably face some trouble before you'd finally remember that - wait, you forgot to send thecsrf_token
. This is a known beginner roadblock in AJAX-Django integration, but after you learn how to make it play nice, it's easy as pie.That's everything that comes to my head. It's a vast subject, but yeah, there's probably not enough examples out there. Just work your way there, slowly, you'll get it eventually.