15

我第一次在 Django 中创建 Python 应用程序。我知道我必须取消注释 urls.py 中的管理工具,我已经这样做了。我也加了autodiscover。每次我尝试向管理面板添加新功能时,都会收到此错误:

“NameError:名称'admin'未定义”

这是我在模型中使用以添加到管理面板的代码:

class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3

    class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]

这是我正在使用的python终端中的代码

admin.site.register(Poll, PollAdmin)

这是我的 urls.py 中的代码:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'iFriends.views.home', name='home'),
    # url(r'^iFriends/', include('iFriends.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    )

有人知道为什么找不到管理员名称吗?

编辑

这是我的整个模型文件:

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __unicode__(self):
        return self.question
    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

    class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField()
    def __unicode__(self):
        return self.choice_text


#COMMENTED OUT UNTIL I FIX THE ADMIN NAME
from django.config import admin

class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3

    class PollAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]

#ADD THIS TO THE MAIN PYTHON FUNCTION
admin.site.register(Poll, PollAdmin)
4

7 回答 7

10

from django.config import admin应该from django.contrib import admin

于 2012-09-07T04:52:10.833 回答
6

**url.py**文件顶部,添加以下代码

from django.contrib import admin
admin.autodiscover()

因此,该特定块应如下所示


from django.conf.urls import patterns, include, url
**from django.contrib import admin
admin.autodiscover()**

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'examplesite.views.home', name='home'),
    # url(r'^examplesite/', include('examplesite.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    #url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
**url(r'^admin/', include(admin.site.urls)),**

)
于 2013-05-03T21:06:30.070 回答
2

我像这样更改我的 urls.py ......这应该是基本格式

from django.conf.urls import  include, url
from django.contrib import admin

admin.autodiscover()
urlpatterns = [
     url(r'^admin/', include(admin.site.urls)),
    ]
于 2017-11-21T19:34:13.387 回答
1

经过漫长而痛苦的寻求解决这个愚蠢的问题,我终于找到了答案!另一个 Django 程序员遇到了同样的问题,发现了这个:

在 ChoiceInLine 的父级中(您将在本教程中看到“admin.StackedInline”),StackedInline 中的 L 不应该大写……就这么简单……非常感谢 Karen Tracey……

http://grokbase.com/p/gg/django-users/133v4wx0sx/django-1-5-tutorial-error-module-has-no-attribute-stackedinline

于 2013-08-01T00:07:13.957 回答
0

我怀疑您from django.contrib import admin的模型文件中没有定义ModelAdmin类的class ChoiceInline(admin.StackedInline):.

于 2012-09-07T03:59:56.427 回答
0

您应该将管理员代码(#COMMENTED OUT 直到我修复管理员名称之后的所有内容)放到 admin.py

于 2012-09-07T04:47:29.880 回答
0

为我解决这个问题的一种方法是在开头添加这个admin.py

from django.contrib import admin
于 2020-07-20T15:31:44.010 回答