Django Cannot import name views

和自甴很熟 提交于 2019-12-11 02:46:42

问题


So I'm trying to get up to speed on Django for a side project by doing the "first Django app" thing at DjangoProject, but I'm getting this weird message when I try to set up views for the first time.

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

It says that the error is in my root/polls/admin.py on line two, and seems to complain about not being able to import "views". The problem is that I am not trying to import views in

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)

The error message also indicates that the issue is the import call to the Choice model, but that doesn't call views either.

#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

Any ideas as to how to fix this?

Update:

#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')
)

UPDATE #2: Oddly enough if I comment out the entire admin.py file, and redefine the from polls import views to import views then it works, but in doing so I've killed my admin section. Any ideas?


回答1:


You have in your address bar:

http://localhost:8000/index

But your urls.py says:

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

So, Django is actually trying to look up:

http://localhost:8000/

That's why it can't find the first URL.




回答2:


OK so I "fixed" the bug although I don't know exactly why this works.

In admin.py I changed the imports from

from django.contrib import admin
from polls.models import Choice
from polls.models import Poll

to

import views
from django.contrib import admin
from models import *

Perhaps it has to do with file organization? Anyways, thanks for the comments guys.




回答3:


Had same problem:
On Windows7, IDE IntelliJ IDEA
in models.py
When imports were like:

from django.db import models

from django.utils import timezone
import datetime

I received error:

ImportError: cannot import name Poll

When I changed imports (using code reformatting in IntelliJ Ctrl+Alt+l) to:

import datetime

from django.db import models

from django.utils import timezone

Everything started working magically :)



来源:https://stackoverflow.com/questions/19168265/django-cannot-import-name-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!