You will often come across problems in your Django applications that are not immediately clear. When I get stuck with a tough bug, especially when it's inside a Django view, I use the Python debugger, which is built into Python, to step through my view code and debug the problem. To do so, you'll need to put this line of code into your view right before the point where you think the problem exists:
import pdb; pdb.set_trace()
Then, the next time you load the page associated with that view, you'll see that your browser appears to not load anything. This is because your Django application is now paused. If you look in the console where you ran the runserver
command, you should see a prompt for pdb
. In the prompt, you can type the name of any variable available in the current Python scope (usually the scope of the view that you are debugging) and it will print the current value of that variable. You can also run a bunch of other debugging commands. For a complete list of available features, take a look at the documentation for the Python debugger at https://docs.python.org/3/library/pdb.html.
A good Stack Overflow question with useful answers that lists some other debugging techniques is http://stackoverflow.com/questions/1118183/how-to-debug-in-django-the-good-way.