2012-01-26 61 views
0

我剛剛完成Django的教程(第四部分),但是當我去檢查服務器一切都完成後,我得到這個請求的觀點:Django的 - 獲取ViewDoesNotExist在未通過代碼

呈現時捕獲ViewDoesNotExist:在模塊polls.views中嘗試結果。錯誤是: '模塊' 對象沒有屬性 '結果'

和通常的:在_get_callback

/usr/lib/pymodules/python2.7/django/core/urlresolvers.py,線路170

本教程的最後一點是擺脫所有請求,除了來自views.py和我的「投票」,儘管我做得很對,但顯然不是。

在代碼中沒有任何地方需要查看「結果」,從我可以看到的情況看,我經歷了幾次代碼,找不到問題。

urls.py:

from django.conf.urls.defaults import patterns, include, url 

from django.contrib import admin 
admin.autodiscover() 

url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 

url(r'^admin/', include(admin.site.urls)), 

from django.conf.urls.defaults import * 
from django.views.generic import DetailView, ListView 
from polls.models import Poll 

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

views.py:

from django.http import HttpResponseRedirect, HttpResponse 
from polls.models import Choice, Poll 
from django.shortcuts import render_to_response, get_object_or_404 
from django.http import Http404 
from django.template import RequestContext 
from django.core.urlresolvers import reverse 

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): 
     # Redisplay the poll voting form. 
     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,))) 

models.py:

from django.db import models 

class Poll(models.Model): 
    question = models.CharField(max_length=200) 
    pub_date = models.DateTimeField('date published') 
    def __unicode__(self): 
     return (self.question) 

class Choice (models.Model): 
    poll = models.ForeignKey(Poll) 
    choice = models.CharField(max_length=200) 
    votes = models.IntegerField() 
    def __unicode__(self): 
     return (self.choice) 

admin.py:

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

class ChoiceInline(admin.TabularInline): 
    model = Choice 
    extra = 3 

class PollAdmin(admin.ModelAdmin): 
    fieldsets = [ 
     (None,    {'fields': ['question']}), 
     ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), 
    ] 
    inlines = [ChoiceInline] 
    list_display = ('question', 'pub_date') 
    list_filter = ['pub_date'] 
    date_hierarchy = 'pub_date' 
    search_fields = ['question'] 


admin.site.register(Poll, PollAdmin) 

任何幫助找到問題表示讚賞。 :)

+1

全堆棧跟蹤可以獲取更多信息...還有你重新啓動服務器? – demalexx

+0

嘗試在乾淨的環境中重現。這聽起來像是你的'PYTHONPATH'中的一個車載模塊。 –

+1

讓我懷疑這是否是從一個較早的狀態持有。你運行'manage.py syncdb';你有沒有重新啓動服務器;你的數據庫結構是否與你的'models.py'匹配。如果數據庫匹配你的'models.py'文件,我傾向於刪除'.pyc'文件並重新啓動apache,或者在另一個設置'pkill python'爲我工作。 – sgallen

回答

0

urlconf的最後一行是polls.views.vote。這將導致Django嘗試導入polls.views,如果該文件中存在任何錯誤(根據錯誤消息得到),它將會失敗。

進入polls/views.py並找到正在導入「結果」的位置。當然,你爲views.py發佈的代碼沒有這樣的內容,但是它是完整的文件還是隻是一個片段?