Michael
Michael

Reputation: 4461

Urlpatterns and Django Debug Toolbar

Django 1.11

I'm reading "Two Scoops of Django". Namely about multiple settings files and multiple requirements files.

So, it is recommended to separate base.py, local.py and production.py.

And for base.py:

INSTALLED_APPS += ("debug_toolbar", )

What troubles me is that Django Debug Toolbar also needs some addition to urls.py:

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

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

http://django-debug-toolbar.readthedocs.io/en/stable/installation.html#urlconf

That is we should either accept this mix of debug and production notions or separate them into multiple files.

I investigated this question. For example, this link: Django: Multiple url patterns starting at the root spread across files

But I'm still somehow suspicious.

Could you give me a piece of advice: what is the best practice of how separate this Django Debut Toolbar from production in urlpatterns.

Upvotes: 0

Views: 1019

Answers (1)

nik_m
nik_m

Reputation: 12086

There's nothing wrong with this approach since you'll only have one URL conf file that will live inside your project root. So the if settings.DEBUG: part, inside your project's root urls.py file looks fine with me.

However, if you want to completely seperate the local and production urls, then you could do it this way:

  1. Create a python module for urls (just as you would do for the settings)
  2. Inside that dir, create your base_urls.py, local_urls.py and production_urls.py
  3. Inside the base_urls you should put the "global" urls that'll be available for both local and production. Inside the local_urls put any additional local urls (like django-debug-toolbar). And finally, in your production put... eh the production urls. Of course, local_urls.py and production_urls.py files should have from .base_urls import * at the top.
  4. After that, you should change the ROOT_URL_CONF setting inside the settings/local.py file to be "project.urls.local_urls" and inside the settings/production.py file change (again) the ROOT_URL_CONF setting to "project.urls.production_urls".

Now, you have separate urls files, for each environment.

Upvotes: 1

Related Questions