Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

django

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.

like image 432
kaboom132 Avatar asked Dec 17 '25 17:12

kaboom132


2 Answers

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 ;)

like image 166
doniyor Avatar answered Dec 20 '25 10:12

doniyor


If you are using the runserver command, you will see the output of print-statements in that command prompt, like a live logging.

like image 34
Mischback Avatar answered Dec 20 '25 10:12

Mischback



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!