Reputation: 3271
I am developing my first django website.
I have written code in my view layer (the handlers that return an HttpResponse object to the view template (hope I am using the correct terminology).
In any case, I want to put print statements in my views.py file, so that I can debug it. However, it looks like stdout has been redirect to another stream, so I am not seeing anything printed out on my console (or even the browser).
What is the recommended way (best practice) for debugging django view layer scripts?
Upvotes: 4
Views: 7959
Reputation: 10444
pip install django-debug-toolbar
and follow the instructions to configure it in: https://github.com/django-debug-toolbar/django-debug-toolbarimport logging
Use the logging to debug: logging.debug('My DEBUG message')
Here is how it works on my class view:
from django.views.generic import TemplateView
import logging
class ProfileView(TemplateView):
template_name = 'profile.html'
def get(self, request, *args, **kwargs):
logging.debug(kwargs)
return render(request, self.template_name)
Upvotes: 0
Reputation: 714
Try django-sentry. Especially if your project is in production stage.
Upvotes: 1
Reputation: 131
I'd upvote dysmsyd, but I don't have the reputation. pdb is good because it lets you step thru your procedure and follow the control flow.
If you are using the django runserver, you can print to stdout or stderr. If you are using the mod_wsgi, you can print to stderr. The pprint module is also useful:
import sys
from pprint import pprint
def myview(request):
pprint (request, sys.stderr)
Upvotes: 1
Reputation: 600059
Use the Python logging
module. Then use the Django debug toolbar, which will catch and display all the things you send to the log.
Upvotes: 2
Reputation: 2266
there are more advanced ways of doing it, but i find dropping
import pdb
pdb.set_trace()
does the job.
Upvotes: 3