2

所以我试图通过在 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 viewsimport views那么它就可以工作了,但是这样做我已经杀死了我的管理部分。有任何想法吗?

4

3 回答 3

1

您的地址栏中有:

http://localhost:8000/index

但是你的 urls.py 说:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index')
)

所以,Django实际上是在尝试查找:

http://localhost:8000/

这就是为什么它找不到第一个 URL。

于 2013-10-04T00:01:28.550 回答
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 *

也许它与文件组织有关?无论如何,感谢评论的家伙。

于 2013-10-05T20:18:45.110 回答
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++中的代码重新格式化Altl更改为:

import datetime

from django.db import models

from django.utils import timezone

一切都开始神奇地工作:)

于 2014-05-19T15:05:37.603 回答