Skip to main content

The First Big Error

So everything was going fine till now and I was progressing in my project, but as we know that coding is riding a bike in Jurassic Park, where you can encounter a dinosaur like errors. I encountered an error in my code. First I will tell you the background details that are necessary to know to be clear about the error occurred, then I will tell you about the error and then we would go to how I solved it.

So all of you must have read about how I was maintaining the user profile if not you can read it here "The User Profile". I would insist on going back to this link again as I have uploaded an image to make things even more clear about how did it. So I had additional fields on my user profile such as image, email, status, city etc. I have used Django signals to create user profile whenever a user is created. To those who don't know what signals are, Signals are built-in in Django which are used to execute some lines of code when some other line of code is executed. This could be compared to event-handling in JavaScript. That is the most similar thing, I could relate signals to or I think are similar to signals. There are many signals in Django, some of them are "pre_save","post_save","pre_delete","post_delete" etc if you wish to read more about Django signals then you can go to this link. I will explain you some so you would have an understanding of what is happening. let's take "post_save" for example, as the name itself indicates it is a signal that is generated when the saving of something occurs. While using signals we specify "senders", these are the objects that send the signals and we have to pass a function that is to be executed when this signal is received. In our case, the sender is the User module of "django.contib.auth", hence whenever a user is saved to the database a signal is raised. Now we define a function in which we create a user profile in which the user is the user which is saved to the database. We have to connect this function and the sender and this is done using the post_save objects connect method. For a clear understanding, you can have a look at the code below

That was the background part you must know to understand what was wrong, don't scratch your head right away understanding what went wrong will require time until you are an expert at Django. So the error was whenever I created a user I was redirected to an error page which said: "Integrity error: Duplicate email". The integrity error is raised whenever we are saving some record which has an attribute similar to the attribute of the other record and that attribute is meant to be unique for every entry. For a minute I thought that I in a hurry have given similar email id. I reentered the user details and I was given an error that a user with the similar username was already existing. I was confused and hence I opened Django's administration page and saw the user was created, I was wired and I tried signing up another user and I was again confronted by the same error. The image of the error is given below.

In the beginning, I didn't know where things were going wrong. It was obvious that something was wrong with the creation of the user. So I opened the debugger and put a break-point at the post method of the user creation form. Debugging it for a while I came to know that there was something wrong with the "save" method. I stepped into the "save" method. Sometimes you can appreciate the beauty of modular code where each and every functionality or feature of the code either small or bigger are written either in a different file or a different function or sometimes in a function of the different module but while debugging a code this doesn't seem so beautiful. In this process of debugging and especially when you are an ammeture debugger as I am, you will end up in a situation where you don't know in which part of the code you are or what is the line you are debugging is meant for and how it is related to the part you are debugging and all of this could be so frustrating.
one of my teacher K.C. sir once told us that if you want to be really good at debugging then you must master patience and you must do it a lot. So for following what he said I was quite patient and debugged my code many times. Debugging for hours together, I went deep inside the codes and somehow was able to find out that something wrong but it's not the "save" function, it was the "create_profile" method which I connected to the "post_save" signal. I was so tired and frustrated as well because I was debugging for hours together and the only thing I found was that something was wrong with another method. I was so tired for the day so I slept that night with a minute head ace. Next day, I started fresh from the "create_profile" method. I debugged it many a time and tried to change many things but nothing happened. At one point, I thought that something went wrong with the database and I dropped my complete database but it did not change a thing. I even thought that the email id is not being saved but that was also not the problem and this continued for a couple of hours. Whenever  I created a user I was getting the same error, the user was created but the profile was not. I tried others method for saving the user but all of them resulted in the same error. the code was working fine without the "post_save" signal but I knew that I want to use it and will somehow have to resolve this error

After all of this happened and took a small break to refresh my mind and came back. I was thinking about something and that was "This error occurs when we enter the same email id for two different users why don't I try adding a user with the email id which is previously present in the database ". I did it and got the same error but this time it was a bit different, the user was also not created. I was sure that if there was an integrity error with the user table then the record will not be created but in my case, the record was created. All of a sudden something struck my mind and I opened the user profile model and there was an additional field "email" all this time the problem was me trying to save the email id for the same user twice and that resulted in an integrity error. I changed the models and run my code and this time both the user and its profile are created without an error. and thus I debugged the first big error of my code. That's it for today stay tuned for more stories about my project. Until then signing off.

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,

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