如果没有admin.autodiscover()输入urls.py,则会显示管理页面You don't have permission to edit anything(参见SO线程).
为什么会这样?如果您总是需要admin.autodiscover()使用管理员添加编辑信息,即使您拥有安全性的超级用户名和密码,为什么Django开发人员不会admin.autodiscover()自动触发?
Ala*_*air 17
在Django 1.7之前,建议将admin.autodiscover()调用放在urls.py中.这允许在必要时禁用它.要求admin.autodiscover()而不是自动调用它是Python哲学的一个例子,"明确比行动更好".请记住,该django.contrib.admin应用程序是可选的,它并未安装在每个站点上,因此始终运行自动发现是没有意义的.
大多数情况下,自动发现的效果非常好.但是,如果您需要更多控制权,则可以手动导入特定应用程序的管理文件.例如,您可能希望在每个管理站点中注册具有不同应用程序的多个管理站点.
应用程序加载在Django 1.7中进行了重构.将autodiscover()被转移到管理应用程序的默认应用程序配置.这意味着自动发现现在在加载管理员应用程序时运行,并且无需添加admin.autodiscover()到您的urls.py. 如果您不想要自动发现,现在可以使用SimpleAdminConfig替代方法禁用它.
K Z*_*K Z 13
(编辑:在Django 1.7+之后已经过时,没有必要,请参阅Alasdair的回答)
我认为这是关于给你更好的控制.考虑以下代码contrib.admin.autodiscover:
def autodiscover():
"""
Auto-discover INSTALLED_APPS admin.py modules and fail silently when
not present. This forces an import on them to register any admin bits they
may want.
"""
import copy
from django.conf import settings
from django.utils.importlib import import_module
from django.utils.module_loading import module_has_submodule
for app in settings.INSTALLED_APPS:
mod = import_module(app)
# Attempt to import the app's admin module.
try:
before_import_registry = copy.copy(site._registry)
import_module('%s.admin' % app)
except:
# Reset the model registry to the state before the last import as
# this import will have to reoccur on the next request and this
# could raise NotRegistered and AlreadyRegistered exceptions
# (see #8245).
site._registry = before_import_registry
# Decide whether to bubble up this error. If the app just
# doesn't have an admin module, we can ignore the error
# attempting to import it, otherwise we want it to bubble up.
if module_has_submodule(mod, 'admin'):
raise
Run Code Online (Sandbox Code Playgroud)
因此,它将自动加载INSTALLED_APPS admin.py模块,并在未找到时以静默方式失败.现在,有些情况下您实际上不希望使用自己的情况AdminSite:
# urls.py
from django.conf.urls import patterns, url, include
from myproject.admin import admin_site
urlpatterns = patterns('',
(r'^myadmin/', include(admin_site.urls)),
)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您不需要打电话autodiscovery().
还有一些时候,您只想通过管理员查看或编辑项目的应用程序子集,并且调用autodiscovery()不会使您这样做.
| 归档时间: |
|
| 查看次数: |
9996 次 |
| 最近记录: |