英文:
Can a Django app have a project wide admin panel?
问题
我对Django还很陌生。我主要使用React和NodeJs,并曾稍微涉足过Laravel/PHP。目前我对Django的文件结构感到有些困惑,希望你可以帮忙。
我理解在一个项目中,每个应用可能有自己的admin.py文件,对吗?这是否意味着在每个项目中可能会有多个管理面板?这似乎与其他Web框架不太相同。
admin.py文件是否可以移动到上一级目录(即manage.py所在的目录)或核心应用程序(即settings.py所在的目录)?
一旦我解决了这个疑惑,我想我就可以继续我的任务了。
谢谢你的帮助。
英文:
I'm new to Django. I've come from a React and NodeJs background primarily and have previously dabbled a little bit with Laravel/PHP. Django's file structure is a bit confusing at the moment, and I'm hoping you can help.
Am I right in assuming the each app in a project can have its own admin.py file? Does that mean there are likely to be multiple admin dashboards in each project? It appears to be unlike any of the other web frameworks.
Is it possible to move the admin.py file one level up (where manage.py exists) or the core app (where settings.py exists)?
Once I breakthrough this conflict in my head, I think I can crack on with my task.
Thanks for your help.
答案1
得分: 0
是的,您可以拥有与您开发的应用程序一样多的管理面板,如果您愿意的话。只要您在每个应用程序的url.py文件中包含管理URL,按照惯例,您可以在localhost:8000/admin/应用程序名称下访问它们(只要您之前设置了URL配置)。但是,如果您希望拥有一个单一的管理面板,其中包含所有应用程序的模型,您必须在通常位于核心文件夹(或应用程序)中的同一个admin.py文件中注册它们,为其他应用程序提供服务。例如,在项目的主文件夹中(其中位于setting.py文件的位置)。
以下是如何为两个名为' app1 '和' app2 '的应用程序分别设置单独的管理面板的示例,每个应用程序都有自己的三个模型。首先是App1:
# App1 admin.py
from django.contrib import admin
from .models import model1, model2, model3
admin.site.register(model1)
admin.site.register(model2)
admin.site.register(model3)
现在是App2:
# App2 admin.py
from django.contrib import admin
from .models import model4, model5, model6
admin.site.register(model4)
admin.site.register(model5)
admin.site.register(model6)
然后,您可以通过导航到相应的URL来访问每个管理面板。默认情况下,管理面板的URL将是'localhost:8000/admin/App1/'和'localhost:8000/admin/App2/'。
但是,如果您希望所有应用程序的模型都在一个单一的管理面板中,则在位于核心文件夹中的admin.py文件中注册所有模型。再次确保您的URL配置正确设置。以下是一个具有来自两个不同应用程序的模型的单一管理面板的示例:
# 核心文件夹 admin.py
from django.contrib import admin
# 从每个应用程序导入模型
from App1.models import model1, model2, model3
from App2.models import model4, model5, model6
# 注册所有应用程序的模型
admin.site.register(model1)
admin.site.register(model2)
admin.site.register(model3)
admin.site.register(model4)
admin.site.register(model5)
admin.site.register(model6)
在上面的代码中,您将在登录为超级用户后以'localhost:8000/admin'访问您的管理面板。
英文:
Yes, you can have as many admin panels as you have apps developed, if you want. Providing you include the admin URL in each app's url.py file, by convention, you will access them at localhost:8000/admin/name_of_the_app (as long as you have previously set up your URLs conf). Yet, if you want a single admin panel with all your apps' models in it, you have to register them in the same admin.py file which is usually centralized in a core folder (or app) serving other apps. For instance, inside your project's main folder (where the setting.py file is located).
Here's an example of how you can set up separate admin panels for two apps named: 'app1' and 'app2', each with its own three models. First for App1:
# App1 admin.py
from django.contrib import admin
from .models import model1, model2, model3
admin.site.register(model1)
admin.site.register(model2)
admin.site.register(model3)
Now for App2:
# App2 admin.py
from django.contrib import admin
from .models import model4, model5, model6
admin.site.register(model4)
admin.site.register(model5)
admin.site.register(model6)
You can then access each admin panel by navigating to the corresponding URL. By default, the URLs for the admin panels will be 'localhost:8000/admin/App1/' and 'localhost:8000/admin/App2/'.
However, if you want one single admin panel for all apps' models, then you register all your models in the admin.py file located in your core folder. Again, make sure your URLs conf are correctly set. Here is the example for a single admin panel with models from two distinct apps:
# Core folder admin.py
from django.contrib import admin
# Import models from each app
from App1.models import model1, model2, model3
from App2.models import model4, model5, model6
# Register models from all the apps
admin.site.register(model1)
admin.site.register(model2)
admin.site.register(model3)
admin.site.register(model4)
admin.site.register(model5)
admin.site.register(model6)
In the above code, you will access your admin panel at 'localhost:8000/admin after login in as a superuser.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论