Stuck on "ViewDoesNotExist" last step of tutorial part 4

221 views
Skip to first unread message

TuckFrance

unread,
Jun 17, 2011, 9:50:56 PM6/17/11
to Django users
I am using Django 1.3 with SQlite. I got through the entire tutorial
except the last step of part 4.

It says "You can now delete the index(), detail() and results() views
from polls/views.py. We don't need them anymore -- they have been
replaced by generic views."

However when I do this I get the following error:

Environment:


Request Method: POST
Request URL: http://localhost:8000/polls/1/vote/

Django Version: 1.3
Python Version: 2.7.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "c:\Python27\lib\site-packages\django\core\handlers\base.py" in
get_response
111. response = callback(request,
*callback_args, **callback_kwargs)
File "C:\Documents and Settings\ask\Desktop\Stream Coding Project
\Django First Tutorial\mysite\polls\views.py" in vote
24. return HttpResponseRedirect(reverse('poll_results',
args=(p.id,)))
File "c:\Python27\lib\site-packages\django\core\urlresolvers.py" in
reverse
391. *args, **kwargs)))
File "c:\Python27\lib\site-packages\django\core\urlresolvers.py" in
reverse
312. possibilities = self.reverse_dict.getlist(lookup_view)
File "c:\Python27\lib\site-packages\django\core\urlresolvers.py" in
_get_reverse_dict
229. self._populate()
File "c:\Python27\lib\site-packages\django\core\urlresolvers.py" in
_populate
220. lookups.appendlist(pattern.callback, (bits,
p_pattern))
File "c:\Python27\lib\site-packages\django\core\urlresolvers.py" in
_get_callback
170. raise ViewDoesNotExist("Tried %s in module %s.
Error was: %s" % (func_name, mod_name, str(e)))

Exception Type: ViewDoesNotExist at /polls/1/vote/
Exception Value: Tried results in module polls.views. Error was:
'module' object has no attribute 'results'

Jonas Geiregat

unread,
Jun 18, 2011, 6:43:30 PM6/18/11
to django...@googlegroups.com
The view function may have been deleted but the urls.py file (in the root of your project) is still (probably) referring to those functions. So look at your urls.py file and you'll figure it out!

Good luck!

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>


Jonas Geiregat
jo...@geiregat.org

Pengfei Xue

unread,
Aug 15, 2012, 4:01:05 AM8/15/12
to django...@googlegroups.com
there's no function in views.py, you should define that 'index' handler

-- 
Sincerely,
Pengfei Xue





已使用 Sparrow

已使用 Sparrow

在 2012年8月15日星期三,上午11:16,Sky 写道:

I've having a similar problem here using Django 1.4 and I can't quite figure what's wrong. The error reads: Could not import polls.views.index. View does not exist in module polls.views.

urls.py

from django.conf.urls import patterns, include, url
from django.views.generic import DetailView, ListView
from polls.models import Poll

Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
url(r'^$',
ListView.as_view(
queryset = Poll.objects.order_by('-pub_date')[:5],
context_object_name = 'latest_poll_list',
template_name = 'polls/index.html')),
url(r'^(?P<pk>\d+)/$',
DetailView.as_view(
model = Poll,
template_name = 'polls/detail.html')),
url(r'^(?P<pk>\d+)/results/$',
DetailView.as_view(
model = Poll,
template_name = 'polls/results.html'),
name='poll_results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)

urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

views.py
# Create your views here.
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from polls.models import Choice, Poll

def vote(request, poll_id):
p = get_object_or_404 (Poll, pk = poll_id)
try:
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render_to_response('polls/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
}, context_instance = RequestContext(request))
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('poll_results', args = (p.id,)))

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/bWqxut6y_YgJ.

Melvyn Sopacua

unread,
Aug 15, 2012, 9:45:21 AM8/15/12
to django...@googlegroups.com
On 15-8-2012 10:01, Pengfei Xue wrote:
> there's no function in views.py, you should define that 'index'
> handler

No, he's not using it according to source.
Which means the source is not in sync with what is being run, typically
fixed by restarting the WSGI provider.

--
Melvyn Sopacua

Sky

unread,
Aug 15, 2012, 10:45:32 PM8/15/12
to django...@googlegroups.com
How do I do that? 

I've tried the commands python manage.py syncdb followed by python manage.py runserver several times but they don't work.

Kurtis Mullins

unread,
Aug 15, 2012, 11:03:27 PM8/15/12
to django...@googlegroups.com
This line should be commented out: "Uncomment the next two lines to enable the admin:"

Also, I don't see why it's trying to import 'polls.views.index'. That would be a function named 'index' in your views.py file in the polls directory (also known as a module). Not only does that 'index' method/function not exist, I don't see anywhere in your urls.py that indicates you are trying to load or otherwise reference that method.

So as Melvyn implied, are you sure this source code you presented is exactly the same as you're trying to run? I think he assumed you were using a WSGI Handler -- but runserver should pick up any changes that you make up to the structure automatically.

Can you share the traceback that your web browser shows when you try to access your Django application? Just make sure to hide any private/sensetive data:
http://localhost:8000 after executing 'python manage.py runserver'

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/VTBxdWtZXTYJ.

Sky

unread,
Aug 15, 2012, 11:19:09 PM8/15/12
to django...@googlegroups.com
I'm pretty sure the source is the same as I copied it out from the editor. I'm using sqlite3, not sure if that affects anything. I'm not sure what's wrong as I got the code straight from the tutorial.

The traceback:

  • /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py in get_response
    1.                             request.path_info)
      ...
  • /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in resolve
    1.                     sub_match = pattern.resolve(new_path)
      ...
  • /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in resolve
    1.             return ResolverMatch(self.callback, args, kwargs, self.name)
      ...
  • /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in callback
    1.         self._callback = get_callable(self._callback_str)
      ...
  • /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py in wrapper
    1.         result = func(*args)
      ...
  • /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in get_callable
    1.                     (lookup_view, mod_name))
      ...

Sky

unread,
Aug 15, 2012, 11:19:55 PM8/15/12
to django...@googlegroups.com
Oh, and it works when the url is http://localhost:8000/polls/1/vote/

Kurtis Mullins

unread,
Aug 15, 2012, 11:53:12 PM8/15/12
to django...@googlegroups.com
I just noticed another thing here. But, it could just be related to copying and pasting your source here. See below

On Tue, Aug 14, 2012 at 11:16 PM, Sky <a.technic...@gmail.com> wrote:
I've having a similar problem here using Django 1.4 and I can't quite figure what's wrong. The error reads: Could not import polls.views.index. View does not exist in module polls.views.

urls.py

from django.conf.urls import patterns, include, url
from django.views.generic import DetailView, ListView
from polls.models import Poll

Uncomment the next two lines to enable the admin:

So, first, add a # to the beginning of that line, to make it this:
# Uncomment the next two lines to enable the admin:
 
from django.contrib import admin
admin.autodiscover()


Here, you're going to make a list of patterns. Nothing wrong with that.
 
urlpatterns = patterns('',
    url(r'^$',
        ListView.as_view(
            queryset = Poll.objects.order_by('-pub_date')[:5],
            context_object_name = 'latest_poll_list',
            template_name = 'polls/index.html')),
    url(r'^(?P<pk>\d+)/$',
        DetailView.as_view(
            model = Poll,
            template_name = 'polls/detail.html')),
    url(r'^(?P<pk>\d+)/results/$',
        DetailView.as_view(
            model = Poll,
            template_name = 'polls/results.html'),
        name='poll_results'),
    url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)


Now, below, you've overridden the same URL patterns. (The previous list should, hypothetically, be ignored at this point) This is actually reading the urls.py file from your 'polls' module. I'm not sure if this is really all in the same file or not. If it is, can you show us what's in your polls/urls.py file?
 
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/bWqxut6y_YgJ.

Sky

unread,
Aug 16, 2012, 12:16:40 AM8/16/12
to django...@googlegroups.com
I've moved the urls.py out of mysite as instructed by the tutorial into polls.

My polls/url.py

from django.conf.urls import patterns, include, url
from django.views.generic import DetailView, ListView
from polls.models import Poll

#Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$',
        ListView.as_view(
            queryset = Poll.objects.order_by('-pub_date')[:5],
            context_object_name = 'latest_poll_list',
            template_name = 'polls/index.html')),
    url(r'^(?P<pk>\d+)/$',
        DetailView.as_view(
            model = Poll,
            template_name = 'polls/detail.html')),
    url(r'^(?P<pk>\d+)/results/$',
        DetailView.as_view(
            model = Poll,
            template_name = 'polls/results.html'),
        name='poll_results'),
    url(r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
)

urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),   
url(r'^admin/', include(admin.site.urls)),
)

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

My polls/views.py

# Create your views here.
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from polls.models import Choice, Poll

def vote(request, poll_id):
p = get_object_or_404 (Poll, pk = poll_id)
try: 
selected_choice = p.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
return render_to_response('polls/detail.html', {
'poll': p,
'error_message': "You didn't select a choice.",
}, context_instance = RequestContext(request))
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('poll_results', args = (p.id,)))


Reply all
Reply to author
Forward
0 new messages