kaboom132 kaboom132 - 1 year ago 68
Python Question

How do I print a list from views.py to the console?

I'm a near-total newbie to Django, and am trying to debug a piece of code which is returning a null list to a template even though it should be returning a list with items in it. Is there any way to print the list to the console from within views.py for debugging purposes? I obviously can't run

python views.py
as the info I want is stored in a SQLite database, but if I try to include a
print
statement within the view I'm using, nothing prints when I refresh the page (and the server contacts my code). Is there a solution? I've looked around for a long time and can't find anything.

Answer Source

Firstly, you never run Django code with python views.py

However, you can see each HTTP request in your console that you open to start your local Django webserver.

If you do e.g. in your index() view function print, you should be able to see the output directly in your console:

def index(request):
   a_list = [1,2,3,4]
   print a_list
   return render(request, 'index.html', {'list':a_list})

ask if something is unclear to you ;)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download