Without putting admin.autodiscover()
in urls.py the admin page shows You don't have permission to edit anything
(See SO thread).
不需要将admin.autodiscover()放在url中。管理页面显示您没有编辑任何内容的权限(请参阅线程)。
Why is this so? If you always need to add admin.autodiscover()
to edit information using the admin even though you have a superuser name and password for security why didn't the Django developers trigger admin.autodiscover()
automatically?.
为什么会这样呢?如果您总是需要添加admin.autodiscover()来使用admin编辑信息,即使您拥有用于安全性的超级用户名和密码,那么为什么Django开发人员没有自动触发admin.autodiscover()呢?
3 个解决方案
#1
11
(edit: Obsoleted after Django 1.7+, not necessary more, see Alasdair's answer)
(编辑:在Django 1.7+之后被淘汰,没有必要更多,请看Alasdair的答案)
I think it's about giving you finer control. Consider the code of contrib.admin.autodiscover
:
我认为这是给你更好的控制。考虑scheme的代码。
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
So it will automatically load the INSTALLED_APPS admin.py modules and fail silently when not found. Now, there are cases when you actually don't want that such as when using your own AdminSite
:
因此它将自动加载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)),
)
in this case, you don't need to call autodiscovery()
.
在本例中,不需要调用autodiscovery()。
There are also other times when you only want to see or edit a subset of apps of your projects through admin, and calling autodiscovery()
would not enable you to do that.
有时,您只希望通过admin查看或编辑项目的应用程序子集,而调用autodiscovery()则不能实现这一点。
#2
13
Before Django 1.7, the recommendation was to put the admin.autodiscover()
call in urls.py. That allowed it to be disabled if necessary. Requiring admin.autodiscover()
instead of calling it automatically was an example of the Python philosophy 'Explicit is better than implicit' in action. Remember, the django.contrib.admin
app is optional, it is not installed on every site, so it wouldn't make sense to always run autodiscover.
在Django 1.7之前,建议将admin.autodiscover()调用放在urls.py中。这允许在必要时禁用它。需要admin.autodiscover()而不是自动调用它,这是Python哲学“显式优于隐式”的一个例子。记住,django.contrib。管理应用程序是可选的,它不是安装在每个站点上,所以总是运行自动发现是没有意义的。
Most of the time autodiscover works well enough. However if you require more control, you can manually import specific apps' admin files instead. For example, you might want to register multiple admin sites with different apps in each.
大多数时候autodiscover的工作还不错。但是,如果您需要更多的控制,您可以手动导入特定应用程序的管理文件。例如,您可能想要注册多个管理站点,每个站点都有不同的应用程序。
App loading was refactored in Django 1.7. The autodiscover()
was moved to the admin app's default app config. That means that autodiscover now runs when the admin app is loaded, and there's no need to add admin.autodiscover()
to your urls.py. If you do not want autodiscovery, you can now disable it by using the SimpleAdminConfig
instead.
Django 1.7重构了应用程序加载。autodiscover()被移动到管理应用程序的默认应用程序配置。这意味着在加载管理应用程序时自动发现运行,并且不需要添加admin.autodiscover()到您的urls.py。如果您不想要自动发现,现在可以使用SimpleAdminConfig来禁用它。
#3
3
Django doesn't require you to use django.contrib.admin on every site - it's not a core module.
Django不需要你使用django.com。管理每个网站-它不是一个核心模块。
#1
11
(edit: Obsoleted after Django 1.7+, not necessary more, see Alasdair's answer)
(编辑:在Django 1.7+之后被淘汰,没有必要更多,请看Alasdair的答案)
I think it's about giving you finer control. Consider the code of contrib.admin.autodiscover
:
我认为这是给你更好的控制。考虑scheme的代码。
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
So it will automatically load the INSTALLED_APPS admin.py modules and fail silently when not found. Now, there are cases when you actually don't want that such as when using your own AdminSite
:
因此它将自动加载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)),
)
in this case, you don't need to call autodiscovery()
.
在本例中,不需要调用autodiscovery()。
There are also other times when you only want to see or edit a subset of apps of your projects through admin, and calling autodiscovery()
would not enable you to do that.
有时,您只希望通过admin查看或编辑项目的应用程序子集,而调用autodiscovery()则不能实现这一点。
#2
13
Before Django 1.7, the recommendation was to put the admin.autodiscover()
call in urls.py. That allowed it to be disabled if necessary. Requiring admin.autodiscover()
instead of calling it automatically was an example of the Python philosophy 'Explicit is better than implicit' in action. Remember, the django.contrib.admin
app is optional, it is not installed on every site, so it wouldn't make sense to always run autodiscover.
在Django 1.7之前,建议将admin.autodiscover()调用放在urls.py中。这允许在必要时禁用它。需要admin.autodiscover()而不是自动调用它,这是Python哲学“显式优于隐式”的一个例子。记住,django.contrib。管理应用程序是可选的,它不是安装在每个站点上,所以总是运行自动发现是没有意义的。
Most of the time autodiscover works well enough. However if you require more control, you can manually import specific apps' admin files instead. For example, you might want to register multiple admin sites with different apps in each.
大多数时候autodiscover的工作还不错。但是,如果您需要更多的控制,您可以手动导入特定应用程序的管理文件。例如,您可能想要注册多个管理站点,每个站点都有不同的应用程序。
App loading was refactored in Django 1.7. The autodiscover()
was moved to the admin app's default app config. That means that autodiscover now runs when the admin app is loaded, and there's no need to add admin.autodiscover()
to your urls.py. If you do not want autodiscovery, you can now disable it by using the SimpleAdminConfig
instead.
Django 1.7重构了应用程序加载。autodiscover()被移动到管理应用程序的默认应用程序配置。这意味着在加载管理应用程序时自动发现运行,并且不需要添加admin.autodiscover()到您的urls.py。如果您不想要自动发现,现在可以使用SimpleAdminConfig来禁用它。
#3
3
Django doesn't require you to use django.contrib.admin on every site - it's not a core module.
Django不需要你使用django.com。管理每个网站-它不是一个核心模块。