通过 admin.py 在 Django 后台注册自己的路由(对应自己的视图)
要在一个 app 里面的 admin.py 添加如下代码(实际上就一个简化的 model)
# django2\apps\business\admin.py
from django.contrib import admin
from django.contrib.admin import AdminSite
from django.http import HttpResponse
from django.urls import path
from .models import *
from .views import index
# Register your models here.
class DummyModel(models.Model):
class Meta:
verbose_name = "自定义路由"
verbose_name_plural = '自定义路由'
app_label = 'business'
def my_custom_view(request):
return HttpResponse('自定义路由,写自己的内容')
class DummyModelAdmin(admin.ModelAdmin):
model = DummyModel
def get_urls(self):
view_name = '{}_{}_changelist'.format(
self.model._meta.app_label, self.model._meta.model_name)
return [
path('settings/', my_custom_view, name=view_name),
]
admin.site.register(DummyModel, DummyModelAdmin)
# django2\apps\business\__init__.py
default_app_config = "apps.business.apps.BusinessConfig"
# django2\apps\business\apps.py
from django.apps import AppConfig
class BusinessConfig(AppConfig):
name = 'apps.business'
verbose_name = "客户例子"