How to print debug messages to the Django console

With normal Django settings, debug messages aren't output to the console, so you'll have to write the settings in setting.py. You can output with the print statement, but it is healthier to display it with logging, and you can display a lot of information.

Add the following to the last line of setting.py. It seems that the format can be set freely by logging.basicConfig.

setting.py


import logging

# For debugging 
if DEBUG:
    # will output to your console
    logging.basicConfig(
        level = logging.DEBUG,
        format = '%(asctime)s %(levelname)s %(message)s',
    )
else:
    # will output to logging file
    logging.basicConfig(
        level = logging.DEBUG,
        format = '%(asctime)s %(levelname)s %(message)s',
        filename = '/my_log_file.log',
        filemode = 'a'
    )

The output of debug messages can be displayed with logging.debug ().

import logging

def article_edit(request, pk):
    post = get_object_or_404(Article, pk=pk)
    if request.method =="POST"
        #Print debug messages to the console
        logging.debug('debug message')
        if form.is_valid():
            post = form.save(commit=False)
            post.author = request.user
            post.published_date = timezone.now()
            post.save()
            return redirect('article_detail', pk = post.pk)
    else:
        form = ArticleForm(instance=post)
    return render(request, 'blog/article_edit.html', {'form' : form})

Please refer to the following for details of logging. https://docs.djangoproject.com/en/1.11/topics/logging/

Recommended Posts

How to print debug messages to the Django console
How to overwrite the output to the console
How to print characters to the console before booting on ARM
How to check the version of Django
How to get colored output to the console
How to operate Linux from the console
How to debug selenium
[Django] How to redirect unlogged-in users to the login page
How to use the generator
How to use the decorator
How to increase the axis
How to start the program
How to write custom validations in the Django REST Framework
How to debug the Python standard library in Visual Studio
How to generate a query using the IN operator in Django
How to do the initial setup from Django project creation
In Django, how to abbreviate the long displayed string as ....
How to calculate the autocorrelation coefficient
How to use the optparse module
[Django] How to test Form [TDD]
How to read the SNLI dataset
How to get the Python version
[Python] How to import the library
How to reflect CSS in Django
How to get started with Django
Deploy the Django tutorial to IIS ①
How to write Django1.9 environment-independent wsgi.py
How to use the ConfigParser module
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to do arithmetic with Django template
How to display the progress bar (tqdm)
How to use the Spark ML pipeline
How to set the server time to Japanese time
How to manually update the AMP cache
How to use the Linux grep command
I want to use the Django Debug Toolbar in my Ajax application
Django initial setup up to Intellij Debug
How to access the Datastore from the outside
How to use the IPython debugger (ipdb)
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
How to build an application from the cloud using the Django web framework
How to pass values to JavaScript variables directly from the [Django] template tag
How to make only one data register on the Django admin screen
How to uniquely identify the source of access in the Django Generic Class View
How to count the number of elements in Django and output to a template
I'm addicted to the difference in how Flask and Django receive JSON data
How to add pre-save processing when adding objects on the Django admin site
How to assign multiple values to the Matplotlib colorbar
How to calculate the volatility of a brand
How to use the C library in Python
How to specify the launch browser for JupyterLab 3.0.0
How to find the area of the Voronoi diagram
How to use MkDocs for the first time
The easiest way to get started with Django
How to specify the NIC to scan with amazon-dash
How to implement Rails helper-like functionality in Django
[Python] How to change the date format (display format)
[Django] How to resolve errors when installing mysqlclient
How to set Django DB to mongodb visual studio 2019