[PYTHON] Install django-extensions and use runserver_plus, which is more convenient than runserver

Thing you want to do

Debug on the Django error screen (browser). Enables the use of interactive shells.

Use the libraries django-extensions and Werkzeug.

What are django-extensions?

django-extensions extends the functionality of manage.py, and there are many other commands.

What is Werkzeug?

Werkzeug is a comprehensive WSGI web application library. It started out as a simple collection of various utilities for WSGI applications and has become one of the most advanced WSGI utility libraries.

https://werkzeug.palletsprojects.com/en/1.0.x/

It's something that makes WSGI easy to use.

What is WSGI?

WSGI [1] is not a server, Python module, framework, API, or any kind of software. This is just an interface specification for the server and application to communicate.

http://wsgi.tutorial.codepoint.net/intro

It seems that there was a purpose to unify the interface because various communication standards of Python came out and there were different communication standards.

Installation

Install

$ pip install django-extensions Werkzeug

Just add to settings.py

pj_name/settings.py


.
.
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django_extensions', # <-Postscript
]
.
.

code

For example, in a view that just displays the string ‘test’

views.py


from django.shortcuts import render
from django.http import HttpResponse


def test(request):
    message = 'test'
    return HttpResponse(message)

If you mistype the variable message as mesage

views.py


from django.shortcuts import render
from django.http import HttpResponse


def test(request):
    message = 'test'
    return HttpResponse(mesage) # <-Typo

Run

runserver

First, run the usual runserver.

$ python manage.py runserver

Go to http://127.0.0.1:8000/. Such an error screen.

スクリーンショット 2020-08-21 12 21 41

runserver_plus

Now run runserver_plus. Just change runserver to runserver_plus. You will use the Debugger PIN later.

$ python manage.py runserver_plus

...
 * Debugger is active!
 * Debugger PIN: 125-696-991 # <-this

It looks like this with runserver_plus.

スクリーンショット 2020-08-21 11 46 30

Mouse over the code area and a terminal icon will appear. Click it.

スクリーンショット 2020-08-21 12 10 42

Enter your Debugger PIN

スクリーンショット 2020-08-21 12 13 02

You will be able to use an interactive shell.

スクリーンショット 2020-08-21 12 14 31

The end

Thank you for reading until the end.

Recommended Posts

Install django-extensions and use runserver_plus, which is more convenient than runserver
Use the pushd command, which is more convenient than the cd command, to instantly return to the original directory.
How to install and use Tesseract-OCR
How to install and use Graphviz