我正在第一次在Django中創建一個Python應用程序。我知道我必須取消對urls.py中的管理工具的評論,我已經這樣做了。我還添加了autodiscover
。每次我嘗試了新的功能添加到管理面板,我得到這個錯誤:Python錯誤:未定義名稱'admin'
「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)
是「Django的.contrib.admin'在INSTALLED_APPS中? – Marat
是的,是的。我已經使用相同的方法在管理面板中添加了「民意調查」部分。現在不起作用 –