所以我试图通过在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
,那么它可以工作,但是这样做我已经杀了我的管理部分。有什么想法吗?
答案 0 :(得分:1)
您的地址栏中有:
http://localhost:8000/index
但是你的urls.py说:
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
所以,Django实际上是在努力查找:
http://localhost:8000/
这就是为什么找不到第一个网址。
答案 1 :(得分:1)
好的,所以我“修复”了这个错误,虽然我不确切知道为什么会这样。
在admin.py中,我从
更改了导入from django.contrib import admin
from polls.models import Choice
from polls.models import Poll
到
import views
from django.contrib import admin
from models import *
也许它与文件组织有关?无论如何,感谢评论家伙。
答案 2 :(得分:1)
有同样的问题:
在 Windows7 上,IDE IntelliJ IDEA
在models.py
中
当导入如下:
from django.db import models
from django.utils import timezone
import datetime
我收到了错误:
ImportError: cannot import name Poll
当我更改导入(使用IntelliJ Ctrl + Alt + l 中的代码重新格式化)到:
import datetime
from django.db import models
from django.utils import timezone
一切都开始神奇地工作了:)