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, among others).
How can I achieve this programmatically (ie using settings.py
).
Many thanks.
Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .
Django's Admin is amazing. A built-in and fully functional interface that quickly gets in and allows data entry is priceless. Developers can focus on building additional functionality instead of creating dummy interfaces to interact with the database.
The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.
First, establish a scheme so that your production server can have different settings than your development servers. A simple way to do that is with a source-control-ignored local_settings.py file, but there are many fancier ways to do it.
Then, in your settings.py file, put:
ADMIN_ENABLED = True
and in your production-only settings file, put:
ADMIN_ENABLED = False
Then in your urls.py:
if settings.ADMIN_ENABLED: urlpatterns += patterns('', (r'^admin/(.*)', include(admin.site.urls)), # ..maybe other stuff you want to be dev-only, etc... )
Extending @NedBatchelder 's answer, you might want to use proper if statement
, like this:
if settings.ADMIN_ENABLED is True: ...
And also remove 'django.contrib.admin'
from INSTALLED_APPS = [...]
, and use the same condition:
if settings.ADMIN_ENABLED is True: INSTALLED_APPS.append('django.contrib.admin')
This way the module wont be loaded, and for eg. collectstatic
wont copy unnecessary static files used only in admin (fonts, images, css, js).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With