因此,我試圖通過在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 views
到import views
那麼它的工作原理,但這樣做我殺了奇怪的是我的管理部分。有任何想法嗎?
你有一個'views.py'? – karthikr
請包含'urls.py'和'views.py'。我的猜測是,問題是導入與'/ index' url相關的視圖。 –
添加了請求的文件 – BWStearns