2014-10-10 47 views
0

我正在關注django tutorial,並且在管理界面註冊我的應用程序時遇到了一些麻煩。如何使用django.admin註冊應用程序?

僅供參考,我對記錄的「民意調查」應用程序進行了一些細微的修改。這裏是我的model.py:

import datetime 

from django.db import models 
from django.utils import timezone 

# Create your models here. 

# Survey class groups questions together. 
class Survey(models.Model): 
    survey_name = models.CharField('name', max_length=200) 
    survey_date = models.DateTimeField('date published') 

    def __str__(self): 
     return self.survey_name 

    def was_published_recently(self): 
     return self.survey_date >= timezone.now() - datetime.timedelta(days=7) 

# Question class that may be assigned to a survey. 
class Question(models.Model): 
    survey = models.ForeignKey(Survey) 
    question_text = models.TextField('question') 

    def __str__(self): 
     return self.question_text 

# Choice are individual choices that are linked to questions. 
class Choice(models.Model): 
    question = models.ForeignKey(Question) 
    choice_text = models.CharField('choice', max_length=200) 
    choice_votes = models.IntegerField('votes', default=0) 

    def __str__(self): 
     return self.choice_text 

我的admin.py:

from django.contrib import admin 

# Register your models here. 

class SurveyAdmin(admin.ModelAdmin): 
    fieldsets = [ 
     (None,    {'fields': ['survey_name']}), 
     ('Date Information', {'fields': ['survey_date']}), 
    ] 

admin.site.register(Survey, SurveyAdmin) 

但運行我的網站時,本地開發服務器報告:

NameError: name 'Survey' is not defined 

我的urls.py有autodiscover

編輯:這是我的urls.py:

from django.conf.urls import * # NOQA 
from django.conf.urls.i18n import i18n_patterns 
from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
from django.contrib import admin 
from django.conf import settings 
from cms.sitemaps import CMSSitemap 

admin.autodiscover() 

urlpatterns = i18n_patterns('', 
    url(r'^admin/', include(admin.site.urls)), # NOQA 
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', 
     {'sitemaps': {'cmspages': CMSSitemap}}), 
    url(r'^', include('cms.urls')), 
) 

# This is only needed when using runserver. 
if settings.DEBUG: 
    urlpatterns = patterns('', 
     url(r'^media/(?P<path>.*)$', 'django.views.static.serve', # NOQA 
      {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), 
     ) + staticfiles_urlpatterns() + urlpatterns # NOQA 

回答

1

您必須導入模型admin.py,目前你缺少它。

寫在admin.pyfrom polls.models import Survey

+0

謝謝,我只是來這裏說!儘管非常感謝你。 – 2014-10-10 21:37:19

相關問題