I’m developing a django app. I wish to change the admin url with an environment variable.
Here is how I retrieve it in urls.py:
import os
admin_url = os.getenv('SUPERUSER_URL', 'custom-admin/') # Default is 'custom-admin/' if no key is set
# Also, the environment variable has a slash at the end
In the urlpatterns it is defined as such:
urlpatterns = [
path(admin_url, admin.site.urls),
# more views...
]
Whenever I try to access my admin panel, I can do so with no issue using the new name. But the issue is two things:
- Once I have accessed the admin panel, anytime I try to access a model linked to the panel, it will redirect to the url that django’s original admin namespace associates it self with. E.g. admin/users instead of custom-admin/users
- The second issue is that I can still access the admin panel using the original name ‘admin’
I am not using any custom admin template, I simply have defined a bunch of models to be used with the admin site in admin.py.
Here’s a snippet of my admin.py file, in case you need to see how I’m implementing them:
from django.contrib import admin
from .models import User, Foo
from django.contrib.auth.admin import UserAdmin
class CustomUserAdmin(UserAdmin):
''' Custom User Admin to inherit the user model '''
model = User
# some data
class FooAdmin(admin.modelAdmin):
list_display = ('foo', 'bar')
search_fields = ('foo', 'bar')
admin.site.register(User, CustomUserAdmin),
admin.site.register(Foo, FooAdmin)
I could massively use some guidance here.