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