如何使用django.admin注册应用程序?

时间:2014-10-10 21:18:45

标签: python django django-admin

我正在关注django tutorial,但在使用管理界面注册我的应用时遇到了一些问题。

作为参考,我对记录在案的"民意调查"做了一些改动。应用程序。这是我的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 个答案:

答案 0 :(得分:2)

您必须在admin.py中导入模型,目前您还缺少它。

写在admin.pyfrom polls.models import Survey