2013-10-03 131 views
2

因此,我試圖通過在DjangoProject上執行「第一個Django應用程序」的事情來加速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 

它說,錯誤是在第二行我的根/調查/ 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) 

該錯誤消息也表明問題是進口調用選擇模型,但是這並不來電與意見。

#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那麼它的工作原理,但這樣做我殺了奇怪的是我的管理部分。有任何想法嗎?

+0

你有一個'views.py'? – karthikr

+0

請包含'urls.py'和'views.py'。我的猜測是,問題是導入與'/ index' url相關的視圖。 –

+0

添加了請求的文件 – BWStearns

回答

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 * 

也許它與文件的組織呢?無論如何,感謝評論傢伙。在models.py

Windows7的,IDE IntelliJ IDEA的
當進口,如::

+0

對我來說,它發現錯誤是在views.py中,而不是在urls.py中 –

1

您的地址欄:

http://localhost:8000/index 

但是你的urls.py說:

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

所以,Django的實際上是試圖查找:

http://localhost:8000/ 

這爲什麼它找不到第一個URL。

+0

即使使用localhost:8000/index,它也會返回相同的錯誤。這個URL只是我在轉向SO之前所嘗試的最後一個。上面的代碼有一些問題。 – BWStearns

1

曾有同樣的問題

from django.db import models 

from django.utils import timezone 
import datetime 

我收到的錯誤:

ImportError: cannot import name Poll 

當我改變了impor TS(使用代碼的IntelliJ 按Ctrl + Alt鍵+ 升重新格式化)到:

import datetime 

from django.db import models 

from django.utils import timezone 

一切開始工作神奇:)