BWStearns
BWStearns

Reputation: 2706

Django Cannot import name views

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?

Upvotes: 2

Views: 16675

Answers (3)

pbaranski
pbaranski

Reputation: 24952

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 :)

Upvotes: 1

BWStearns
BWStearns

Reputation: 2706

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.

Upvotes: 1

Aaron Lelevier
Aaron Lelevier

Reputation: 20760

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.

Upvotes: 1

Related Questions