AJAX and Monolithic Django
There is often a huge question about the goodness of django, because of its monolithic nature.
Working with a front-end framework and REST architecture provides the flexibility and stretch of implementing many useful front-end intricacies. But how do we do that using Django and its “share nothing”architecture? How do you update front-end components without reloading pages? This is where AJAX comes handy.
Let us say, you want to save objects on a click or remove them from a model, all of this without reloading. This is how one would go about implementing this in django.
Defining the Database for implementation
Below is the model for the above
Creating Views for the addition of the pin to the user
We define a post function where we get the request and accordingly we do the needful.
Making calls from the client side
Once the back-end is setup, we need to make a call to the API, so that the object is added
Here goes the code for the javascript file which is to be included in the base.html
First get the CSRF_TOKEN from the cookies and then make a call with the token as data
Add the URL path for the Above
url(r'^(?P<slug>[\w-]+)/resources/(?P<resource_slug>[\w-]+)/pin/$', UserPinView.as_view(), name="user_pin")
And you are ready to add objects on a click