英文:
Django 2.2 : Is there any way to remove app name from admin urls?
问题
I am using Django Built-in Admin panel, is there any way to remove app name from urls?
如果我想访问用户列表,它会重定向到 27.0.0.1:8000/admin/auth/user/
,我是否可以使其成为 27.0.0.1:8000/admin/user/
,而不带有应用程序名称 auth
?
谢谢,
英文:
I am using Django Built-in Admin panel, is there any way to remove app name from urls?
If I want to access User listing, it redirects me to 27.0.0.1:8000/admin/auth/user/
can I make it 27.0.0.1:8000/admin/user/
without the app name auth
?
Thanks,
答案1
得分: 2
根据这里的文档,您可以创建一个自定义的AdminSite
并重写get_urls
方法。这段简单的代码应该可以胜任:
在您的common.admin.py
文件中:
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
def get_urls(self):
urlpatterns = super().get_urls()
for model, model_admin in self._registry.items():
urlpatterns += [
path('%s/' % (model._meta.model_name), include(model_admin.urls)),
]
return urlpatterns
my_admin = MyAdminSite('My Admin')
my_admin.register(YourModel)
...
请注意,您需要使用新的自定义AdminSite
实例来注册您的模型。
然后在您的项目的urls.py
文件中:
from common.admin import my_admin as admin
from django.urls import path
urlpatterns = [
path('admin/', admin.urls),
# 您的其他模式
]
英文:
As documented here you can create a custom AdminSite
and override the get_urls
method. This simple code should to the job:
In your common.admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
def get_urls(self):
urlpatterns = super().get_urls()
for model, model_admin in self._registry.items():
urlpatterns += [
path('%s/' % (model._meta.model_name), include(model_admin.urls)),
]
return urlpatterns
my_admin = MyAdminSite('My Admin')
my_admin.register(YourModel)
...
Note that you register your models with the new custom AdminSite
instance.
Then in your projects urls.py
from common.admin import my_admin as admin
from django.urls import path
urlpatterns = [
path('admin/', admin.urls),
# Your other patterns
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论