所以我试图通过在 DjangoProject 上做“第一个 Django 应用程序”的事情来加快 Django 的速度,但是当我第一次尝试设置视图时,我收到了这个奇怪的消息。
ImportError at /index
cannot import name views
Request Method: GET
Request URL: http://localhost:8000/index
Django Version: 1.5.4
Exception Type: ImportError
Exception Value:
cannot import name views
Exception Location: /Users/Dev/Desktop/socialSignIn/socSignInLocater/polls/admin.py in <module>, line 2
Python Executable: /usr/bin/python
Python Version: 2.7.2
Python Path:
['/Users/Dev/Desktop/socialSignIn/socSignInLocater',
'/Library/Python/2.7/site-packages/setuptools-1.1.6-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
Server time: Thu, 3 Oct 2013 15:58:02 -0400
它说错误出现在我的 root/polls/admin.py 第二行,并且似乎抱怨无法导入“视图”。问题是我没有尝试将视图导入
admin.py
#admin.py
from django.contrib import admin
from polls.models import Choice
from polls.models import Poll
# admin.site.register(Poll)
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 1
class PollAdmin(admin.ModelAdmin):
fields = ["pub_date", "question"]
inlines = [ChoiceInline]
list_display = ("question", "pub_date", "was_published_recently")
list_filter = ['pub_date']
sarch_fields = ['question']
date_heirarchy = 'pub_date'
admin.site.register(Poll, PollAdmin)
# admin.site.register(Choice)
该错误消息还表明问题是对 Choice 模型的导入调用,但也没有调用视图。
#models.py
from django.db import models
import datetime
from django.utils import timezone
# Create your models here.
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(default=0)
def __unicode__(self):
return self.choice_text
关于如何解决这个问题的任何想法?
更新:
#views.py
from django.http import HttpResponse
# from django.views.generic.base import TemplateView
def index(request):
return HttpResponse("You found the Poll Index.")
#urls.py
from django.conf.urls.defaults import * #patterns, url
#
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
更新#2:奇怪的是,如果我注释掉整个 admin.py 文件,然后重新定义它from polls import views
,import views
那么它就可以工作了,但是这样做我已经杀死了我的管理部分。有任何想法吗?