Python 错误 : name 'admin' is not defined

标签 python django django-models django-admin django-urls

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

“名称错误:未定义名称‘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)

最佳答案

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

关于Python 错误 : name 'admin' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311390/

相关文章:

python - 在 numpy 数组中围绕已知索引 (x, y) 切片 20×20 区域

Python数据操作: Duplicate and Average row and column values using dates

python - 在应用服务计划下的 azure 功能中发送电子邮件

python - django变量转换为输入文本值

mysql - Django - 操作错误 : (1054, "Unknown column ' xx' 在 'field list' 中“)

python - 在python中合并df

django - 在list_display django中显示子类的信息

ios - Django 和 iOS 中的 CSRF token

Django 迁移卡住了

python - 如何仅在 django 中将外键选择限制为相关对象