自定義Admin後臺的登錄頁面 (1) 在主應用里創建myadmin.py和myapps.py文件,在myadmin.py文件中定義MyAdminSite類,該類繼承父類AdminSite並重寫admin_view()和get_urls()方法從而更改Admin後臺系統地登錄地址。 from dja ...
自定義Admin後臺的登錄頁面
(1) 在主應用里創建myadmin.py和myapps.py文件,在myadmin.py文件中定義MyAdminSite類,該類繼承父類AdminSite並重寫admin_view()和get_urls()方法從而更改Admin後臺系統地登錄地址。
from django.contrib import admin
from functools import update_wrapper
from django.views.generic import RedirectView
from django.urls import reverse
from django.views.decorators.cache import never_cache
from django.views.decorators.csrf import csrf_protect
from django.http import HttpResponseRedirect
from django.urls import include, path, re_path
from django.contrib.contenttypes import views as contenttype_views
from django.contrib.auth.views import redirect_to_login
class MyAdminSite(admin.AdminSite):
def admin_view(self, view, cacheable=False):
def inner(request, *args, **kwargs):
if not self.has_permission(request):
if request.path == reverse('admin:logout', current_app=self.name):
index_path = reverse('admin:index', current_app=self.name)
return HttpResponseRedirect(index_path)
# 修改註銷後重新登錄的路由地址
return redirect_to_login(
request.get_full_path(),
'/user/login.html'
)
return view(request, *args, **kwargs)
if not cacheable:
inner = never_cache(inner)
if not getattr(view, 'csrf_exempt', False):
inner = csrf_protect(inner)
return update_wrapper(inner, view)
def get_urls(self):
def wrap(view, cacheable=False):
def wrapper(*args, **kwargs):
return self.admin_view(view, cacheable)(*args, **kwargs)
wrapper.admin_site = self
return update_wrapper(wrapper, view)
urlpatterns = [
path('', wrap(self.index), name='index'),
path('login/', RedirectView.as_view(url='/user/login.html')), # 修改登錄頁面的路由地址
path('logout/', wrap(self.logout), name='logout'),
path('password_change/', wrap(self.password_change, cacheable=True), name='password_change'),
path(
'password_change/done/',
wrap(self.password_change_done, cacheable=True),
name='password_change_done',
),
path('jsi18n/', wrap(self.i18n_javascript, cacheable=True), name='jsi18n'),
path(
'r/<int:content_type_id>/<path:object_id>/',
wrap(contenttype_views.shortcut),
name='view_on_site',
),
]
valid_app_labels = []
for model, model_admin in self._registry.items():
urlpatterns += [
path('%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
]
if model._meta.app_label not in valid_app_labels:
valid_app_labels.append(model._meta.app_label)
if valid_app_labels:
regex = r'^(?P<app_label>' + '|'.join(valid_app_labels) + ')/$'
urlpatterns += [
re_path(regex, wrap(self.app_index), name='app_list'),
]
return urlpatterns
上述代碼比較固定,只需根據需求更改註釋部分的地址即可。
(2) 將自定義的MyAdminSite類進行系統註冊,由MyAdminSite類實例化創建Admin後臺系統,在myapps.py中定義系統註冊類MyAdminConfig.
from django.contrib.admin.apps import AdminConfig
class MyAdminConfig(AdminConfig):
default_site = 'myblog.myadmin.MyAdminSite'
(3) 在settings.py文件的INSTALLED_APPS中配置系統註冊類MyAdminConfig。
# 需要將原來的 'django.contrib.admin' 註釋掉
INSTALLED_APPS = [
# 'django.contrib.admin',
'myblog.myapps.MyAdminConfig',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]