如何在django.admin注册一个应用?

2024-05-14 19:02:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在跟踪django tutorial,在管理界面注册我的应用程序时遇到一些问题。在

我对“投票”做了些小小的修改。这是我的模型.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

我的管理员py公司名称:

^{pr2}$

但是在本地运行我的站点时,开发服务器会报告:

NameError: name 'Survey' is not defined

我的网址.py有autodiscover。在

编辑:这是我的网址.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

Tags: djangonamefrompyimportselfreturnmodels

热门问题