How can I disable Django's admin in a deployed project, but keep it for local development?

后端 未结 3 1023
悲&欢浪女
悲&欢浪女 2021-01-30 00:57

I am currently working in a Django project for which I need access to the admin area for local development, but want to disable it in the deployed site (for security reasons, am

3条回答
  •  天涯浪人
    2021-01-30 01:50

    @madneon 's answer is terrific but requires an update and a small correction, and unfortunately the suggested edit queue is full.

    For the first part, as it implies the use of @Ned Batchelder 's answer, the use of patterns() is no longer supported in Django 1.9 and above.

    A current implemention could look like:

    from django.conf import settings
    from django.urls import path
    
    urlpatterns = []
    
    if settings.ADMIN_ENABLED is True:
        urlpatterns += [path('admin/', admin.site.urls),]
    
    urlpatterns += [
       # ... Other paths
    ]
    

    For the second part regarding appending to INSTALLED_APPS, this needs to go in the settings.py file and cannot be placed in the urls files.

    As such, it should be written:

    if ADMIN_ENABLED is True:
        INSTALLED_APPS.append('django.contrib.admin')
    

    If you include settings. before ADMIN_ENABLED you'll get an error.

提交回复
热议问题