[PYTHON] [Django] Notes on using django-debug-toolbar

When developing a web application using Django on CentOS built with Vagrant, I was addicted to using django-debug-toolbar, so make a note

environment

Install django-debug-toolbar

#pip install django-debug-toolbar

Installation result

#pip list --format=columns

Package               Version
--------------------- -------
django-debug-toolbar  1.6    

This time 1.6 was installed

Confirmation of REMOTE_ADDR

I think there are various confirmation methods, but I will respond by the following method

example_app/view.py


from django.http import HttpResponse
from django.shortcuts import render
from app1.models import Ipaddress
	
	
def test(request):
    ip_addr = request.META['REMOTE_ADDR']
    return render(request,
        'test.html',
        {'ip_addr' : ip_addr}
        )
        

test.html


{{ ip_addr }}

example_app/urls.py



from django.conf.urls import url
from examle_app import views

urlpatterns = [
    url(r'^test/$', views.test, name='test'),

]

example_project/urls.py


from django.conf.urls import include, url
from django.conf import settings
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^example_app/', include('example_app.urls', namespace = 'example_app')), #Add here
]

If you run server in this state and access http: // IP address: 8000 / example_app / test /, REMOTE_ADDR should be displayed.

Edit setting.py

Check the status of setting.py and add the missing part

setting.py


...

DEBUG = True

INTERNAL_IPS = ('127.0.0.1', 'REMOTE here_Write ADDR',)

...


INSTALLED_APPS = [
    ...
    'debug_toolbar', 
    ...
]

...


MIDDLEWARE = [
    ...
    'debug_toolbar.middleware.DebugToolbarMiddleware',
	...
]

...

STATIC_URL = '/static/'

...

DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TEMPLATE_CONTEXT': True,
}

Edit urls.py on the project side

Add the following to urls.py on the project side

example_project/urls.py


from django.conf import settings

...
...

if settings.DEBUG:
    import debug_toolbar
    urlpatterns += [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ]

Check page

When you start the WEB server and check the page, the debug Toolbar should be displayed on the right side of the screen.

Recommended Posts

[Django] Notes on using django-debug-toolbar
Celery notes on Django
Notes on using Alembic
Notes on optimization using Pytorch
django notes
Django notes
Notes on using MeCab from Python
Notes on using post-receive and post-merge
Notes on installing Python using PyEnv
Notes on using rstrip with python.
Notes on using matplotlib on the server
(Beginner) Notes on using pyenv on Mac
Notes for using OpenCV on Windows10 Python 3.8.3.
[Django] as_view () notes
Notes on creating static files in Django
Notes on Flask
Django Template notes
Notes on implementing APNs tests using Pytest
Notes on using OpenCL on Linux on the RX6800
Notes on using code formatter in Python
[Django] JWT notes
Notes on installing Python3 and using pip on Windows7
Notes on using dict in python [Competition Pro]
Notes for using TensorFlow on Bash on Ubuntu on Windows
[Python] Notes on accelerating genetic algorithms using multiprocessing
Notes on neural networks
Run Django on PythonAnywhere
Notes on installing PycURL
[Personal notes] Python, Django
Try using Django templates.html
Hello World on Django
Notes using Python subprocesses
Notes on SciPy.linalg functions
Build a Django development environment using pyenv-virtualenv on Mac
Miscellaneous notes about deploying the django app on Heroku
Prepare Django development environment using homebrew on MacOSX Mavericks (10.9)
Minimum notes when using Python on Mac (Homebrew edition)
Python notes using perl-ternary operator
Notes on installing dlib on mac
Notes on python's sqlite3 module
Notes on defining PySide slots (2)
Python notes using perl-special variables
Try using OpenCV on Windows
Notes on pyenv and Atom
Notes on defining PySide slots
[Python] Notes on data analysis
DEBUG settings when using Django
Notes on installing Python on Mac
Django environment development on Windows 10
Notes on studying multidimensional scaling
Install Django on your Mac
Process on GPU using chainer.cuda.elementwise
Notes on installing pipenv on Mac
Broadcast on LINE using python
Hello World (beginners) on Django
Notes on installing Anaconda 3 on Windows
[Django] Directory structure practices + notes
Notes on imshow () in OpenCV
Notes on installing Python on CentOS
WEB application development using Django [Django startup]
Launch notes for existing Django applications