Django admin.site.register不添加我的应用程序管理员

2024-04-29 14:47:43 发布

您现在位置:Python中文网/ 问答频道 /正文

作为一个django新手(我与其他python webframework如turbogears和bottle有过一些经验,但我正在探索django),我正在尝试为我的应用程序模型自动创建管理管理

在main URLS.py中,我有:

编辑

from django.contrib import admin
admin.autodiscover()

之后:

urlpatterns = patterns('',
                       url(r'^appname/',include('appname.urls')),
                       url(r'^admin/',include(admin.site.urls)) 

注意,这是在main urls.py中,而不是在app urls.py中

按照教程(在教程中确实对我有用)的要求,我在appname文件夹中创建了一个“admin.py”文件,在那里:

from appname.models import Appname
from django.contrib import admin


class appnameAdmin(admin.ModelAdmin):
        fieldsets = [various field sets and fields etc ]

admin.site.register(Appname,AppnameAdmin)

在seting.py中,我没有注释

'django.contrib.admin'

我没有在命令行窗口中得到任何错误,并且基本管理屏幕确实会出现(auth和sites)

我检查了manage.py shell中admin.py中的导入,一切似乎都正常工作,我还试着注释掉AppnameAdmin类,然后注册:

admin.site.register(Appname) 

但那不起作用

我想我漏掉了一些显而易见的东西-我很乐意帮忙

使用django 1.4+python 2.72


Tags: djangofrompyimportregisterurlincludeadmin
3条回答

检查所有these

There are seven steps in activating the Django admin site:

  1. Add 'django.contrib.admin' to your INSTALLED_APPS setting.
  2. The admin has four dependencies - django.contrib.auth, django.contrib.contenttypes, django.contrib.messages and django.contrib.sessions. If these applications are not in your INSTALLED_APPS list, add them.
  3. Add django.contrib.messages.context_processors.messages to TEMPLATE_CONTEXT_PROCESSORS and MessageMiddleware to MIDDLEWARE_CLASSES. (These are both active by default, so you only need to do this if you’ve manually tweaked the settings.)
  4. Determine which of your application’s models should be editable in the admin interface.
  5. For each of those models, optionally create a ModelAdmin class that encapsulates the customized admin functionality and options for that particular model.
  6. Instantiate an AdminSite and tell it about each of your models and ModelAdmin classes.
  7. Hook the AdminSite instance into your URLconf.
  • 你的已安装应用中是否有所有其他管理依赖项?
  • 你的url.py中有admin.autodiscover()吗?

另外,我认为您的代码应该更像这样:

from projectname.appname.models import Appname
from django.contrib import admin


class AppnameAdmin(admin.ModelAdmin):
        fieldsets = [various field sets and fields etc ]

admin.site.register(Appname,AppnameAdmin)

在模型管理中设置此项:

def has_add_permission(self, request, obj=None):
    return True
def has_change_permission(self, request, obj=None):
    return True
def has_delete_permission(self, request, obj=None):
    return True

啊-我发现了问题。我将admin.py保存在template/appname/文件夹而不是appname/文件夹中。我真蠢。很抱歉打扰你。

相关问题 更多 >