Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug in Django, the good way? [closed]

So, I started learning to code in Python and later Django. The first times it was hard looking at tracebacks and actually figure out what I did wrong and where the syntax error was. Some time has passed now and some way along the way, I guess I got a routine in debugging my Django code. As this was done early in my coding experience, I sat down and wondered if how I was doing this was ineffective and could be done faster. I usually manage to find and correct the bugs in my code, but I wonder if I should be doing it faster?

I usually just use the debug info Django gives when enabled. When things do end up as I thought it would, I break the code flow a lot with a syntax error, and look at the variables at that point in the flow to figure out, where the code does something other than what I wanted.

But can this be improved? Are there some good tools or better ways to debug your Django code?

like image 417
googletorp Avatar asked Jul 13 '09 07:07

googletorp


People also ask

What is pdb in Django?

Django Debugging Using Python Debugger (Pdb) Most basic Django debugging tool is pdb, a part of Python standard library.

What does debug false do Django?

Open your settings.py file (or settings_local.py ) and set DEBUG = False (just add that line if necessary). Turning off the Django debug mode will: Suppress the verbose Django error messages in favor of a standard 404 or 500 error page. You will now find Django error messages printed in your arches.

What is pdb Python?

The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame.

How to debug a Django view function?

Just add following line in to a Django view function: If you try to load that page in your browser, the browser will hang and you get a prompt to carry on debugging on actual executing code. However there are other options (I am not recommending them):

How to debug Django using PTVs?

If you want to debug Django using PTVS, you need to do the following: 1 In Project settings - General tab, set "Startup File" to "manage.py", the entry point of the Django program. 2 In Project settings - Debug tab, set "Script Arguments" to "runserver --noreload". The key point is the "--noreload"... 3 Enjoy it. More ...

How to set breakpoints in Django project?

In Project settings - General tab, set "Startup File" to "manage.py", the entry point of the Django program. In Project settings - Debug tab, set "Script Arguments" to "runserver --noreload". The key point is the "--noreload" here. If you don't set it, your breakpoints won't be hit.

Is there a way to run Django code in Werkzeug?

If you use the django-extensions, you get a runserver_plus managment command which starts the development server and gives you Werkzeug's debugger on exceptions. Of course, you should only run this locally, as it gives anyone with a browser the rights to execute arbitrary python code in the context of the server.


1 Answers

There are a bunch of ways to do it, but the most straightforward is to simply use the Python debugger. Just add following line in to a Django view function:

import pdb; pdb.set_trace() 

or

breakpoint()  #from Python3.7 

If you try to load that page in your browser, the browser will hang and you get a prompt to carry on debugging on actual executing code.

However there are other options (I am not recommending them):

* return HttpResponse({variable to inspect})  * print {variable to inspect}  * raise Exception({variable to inspect}) 

But the Python Debugger (pdb) is highly recommended for all types of Python code. If you are already into pdb, you'd also want to have a look at IPDB that uses ipython for debugging.

Some more useful extension to pdb are

pdb++, suggested by Antash.

pudb, suggested by PatDuJour.

Using the Python debugger in Django, suggested by Seafangs.

like image 188
simplyharsh Avatar answered Sep 17 '22 23:09

simplyharsh