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.
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 ;)
If you are using the runserver command, you will see the output of print-statements in that command prompt, like a live logging.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With