When developing a web application using Django on CentOS built with Vagrant, I was addicted to using django-debug-toolbar, so make a note
#pip install django-debug-toolbar
Installation result
#pip list --format=columns
Package Version
--------------------- -------
django-debug-toolbar 1.6
This time 1.6 was installed
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.
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,
}
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)),
]
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