Skip to main content

Working With AJAX

In the last post, we discussed the error I have got and how I overcame those. In this blog, we will see how I dealt with AJAX. To begin with, all of you have used some social media like facebook, Instagram etc and all of them have some things in common. One of them is the like feature. Yow can like anything you find useful, fun or interesting. That simple "like" feature. Many of the normal users don't observe this actually, they don't have to observe this but whenever you hit the like button or double-tap in Instagram and the information that you liked that post is sent to the server, saved in the databases and some indication is given to you as well, so that you know that you have liked this post. All of this happens without the page getting reloaded. This sounds like usual stuff to the normal people but it's not as simple as it looks like. Not for an amateur web developer who has just started with web developing

Generally, if we wish to make a get request to an URL we make redirect user to that URL and the browser makes the get request to the URL and whatever the work that is to be done can be coded in the get function of the View responsible for handling that URL. So to do this, I took a new URL, created a view for that URL and written my logic for saving that like to the database. I made a like button and whenever the user clicked the button he should be redirected to that URL. It would not be a nice experience if the user has to navigate back to the previous page whenever he/she likes a post. So for this, I have redirected the user to the URL from which he was directed to this URL. While doing this, I came across a new fact. A user can like the post in any page, there is no single page to which the user must be redirected after liking a post. So I was to come up with a way to find out the reference URL from which the user was directed to this URL. While in search of a way, I came to know about the "metadata" every request carries along with it and how that data could be accessed in Python. We can do this by accessing the META object of a request, this looks like this "request.META". META is a dictionary which has many key-value pairs that are necessary for a request and the key which can be used for over case was  "HTTP_REFERER". This contains the URL from which the user was directed. I redirected the user back and this was working pretty well but I knew that this wasn't the way as the page was reloading. So I dug up and found that the thing used for such requests was called AJAX. AJAX stands for Asynchronous Javascript And XML, This makes the request to the URL without redirecting and makes the changes depending on the response received without reloading. It was a little tricky and I got stuck many times but thanks to W3schools, I learnt it and applied it as well and similarly I implemented the "follow" feature in a similar way.

The real problem comes while making a post request to the backend as the Django by default requires CSRF token for every post request. CSRF stands for Cross Site Request Forgery. This token is generated be Django to prevent the user from making unwanted request to the server as well as request from the other applications as well. You can read about this more on the net. So there are many ways to send a CSRF token in the Djangdocumentationtion. We have to do a lot of set up to the ajax request but afterr so much trying none of them seemed to work for me. At the end, I ended up removing the CSRF needed for making a post request, for the view I made for comment. This code is done with the "csrf_extemt" decorator present int the Django library. This decorator can be added to the function based view of the methods of a class-based view. Hence these are the ways I used AJAX in my project. I will still be working on doing the comment thing using CSRF token and if done, I will definitely let you all know. Thanks for being with me till now. I always will be improving my work for you all. Stay tuned.

Comments

Popular posts from this blog

The User Profile

This is my second blog about my Instagram project and till now I am confronted with many questions and problems, two such problems are how to store the user profile and how to implement the "follow" feature of Instagram. In this blog, I will describe the prior problem To any social networking site, the most important component is its end user. The user has his/her important role in the functioning of a social networking site and that part doesn't need a description. Till now, for all my mini projects, whenever the user was required I have used the User model of the Django framework, but this is something different. An SNS(social networking site) always has a user profile which holds a wide variety of information such as status, display picture, email, links to other sites etc but the Django's User model has only some fields namely first name, last name, email, username, password. Only this handful of information cannot account for the user profile. I know many of y

The Follow Feature

In my last blog, I described how I dealt with the user profile issue I faced but it wasn't over, a bigger issue was yet to be solved. Instagram has given us a really good feature which we call "Follow", that lets us follow any other user and see whatever they post or in simpler words whatever they want the world to see. Simple, isn't it? Well in programming point of view, not really. Friends and Followers are the next most important part of an SNS (Social networking site). Why shouldn't it be? Most of the people use them so they could showoff things to other friends, family or someone totally unknown just because it somehow makes them feel valued. What kind of a disorder is that? Well, that's a totally different story which we are not supposed to discuss now. This feature so important because what's the point of calling it a social networking site when you don't have a network. So this problem is solved by me in the following manner To begin with,