2016-03-03 44 views
0

我通過Django的教程製作方式,並在教程5>「測試詳細信息視圖」的Python,Django的,教程,測試詳細信息視圖,錯誤

已經跌進錯誤在我的部分。當我運行:

python manage.py test polls

在終端

,我迎接此消息:

Creating test database for alias 'default'... 
.F....... 
====================================================================== 
FAIL: test_detail_view_with_a_past_question (polls.tests.QuestionIndexDetailTests) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Users/theoford/Documents/python/04_django_intro/mysite/polls/tests.py", line 115, in test_detail_view_with_a_past_question 
    status_code=200) 
    File "/Library/Python/2.7/site-packages/django/test/testcases.py", line 398, in assertContains 
    msg_prefix + "Couldn't find %s in response" % text_repr) 
AssertionError: Couldn't find 'Past Question.' in response 

---------------------------------------------------------------------- 
Ran 9 tests in 0.030s 

FAILED (failures=1) 
Destroying test database for alias 'default'... 

這是對「噸的代碼我有tests.py內相用過去的問題來看詳細視圖'(它是函數的後者)。

class QuestionIndexDetailTests(TestCase): 
    def test_detail_view_with_a_future_question(self): 
     """ 
     The detail view of a question with a pub_date in the future should 
     return a 404 not found. 
     """ 
     future_question = create_question(question_text='Future question.', 
              days=5) 
     response = self.client.get(reverse('polls:detail', 
            args=(future_question.id,))) 
     self.assertEqual(response.status_code, 404) 

    def test_detail_view_with_a_past_question(self): 
     """ 
     The detail view of a question with a pub_date in the past should 
     display the question's text. 
     """ 
     past_question = create_question(question_text='Past Question.', 
             days=-5) 
     response = self.client.get(reverse('polls:detail', 
            args=(past_question.id,))) 
     self.assertContains(response, past_question.question_text, 
          status_code=200) 

我是新來的社區#1,所以我很抱歉,如果這個問題的格式和phraising是不正確,不完整或不恰當的,但是從這個問題提前任何幫助,將不勝感激。

views.py:

from django.shortcuts import get_object_or_404, render 
from django.http import HttpResponseRedirect, HttpResponse 
from django.core.urlresolvers import reverse 
from django.views import generic 
from django.utils import timezone 

from .models import Choice, Question 


class IndexView(generic.ListView): 
    template_name = 'polls/index.html' 
    context_object_name = 'latest_question_list' 

    def get_queryset(self): 
     """Return the last five published questions (not including those published in the future).""" 
     return Question.objects.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[:5] 

class DetailView(generic.DetailView): 
    model = Question 
    template_name = 'polls/detail.html' 

    def get_queryset(self): 
     """ 
     Excludes any questions that aren't published yet. 
     """ 
     return Question.objects.filter(pub_date__lte=timezone.now()) 

class ResultsView(generic.DetailView): 
    model = Question 
    template_name = 'polls/results.html' 

def vote(request, question_id): 
    question = get_object_or_404(Question, pk=question_id) 
    try: 
     selected_choice = question.choice_set.get(pk=request.POST['choice']) 
    except (KeyError, Choice.DoesNotExist): 
     # Redisplay the question voting form. 
     return render(request, 'polls/detail.html', { 
      'question': question, 
      'error_message': "You didn't select a choice.", 
     }) 
    else: 
     selected_choice.votes += 1 
     selected_choice.save() 
     # Always return an HttpResponseRedirect after successfully dealing 
     # with POST data. This prevents data from being posted twice if a 
     # user hits the Back button. 
     return HttpResponseRedirect(reverse('polls:results', args=(question.id,))) 
+0

您可以顯示視圖嗎?問題可能在那裏。 –

+0

當然 - 只是將它添加到問題 –

+0

調試它......在調用'test_detail_with_a_past_question()'前先調用'import pdb; pdb.set_trace()'它會暫停測試並給你一個shell來檢查你的代碼... in那個shell做 'Question.objects.all()'檢查日期......你甚至可以用'n'或者「步入」使用's'逐步執行「 – pleasedontbelong

回答

0

你要檢查 '過去的問題。'在response.content中,而不是response。將test_detail_view_with_a_past_question的最後一行更改爲:

self.assertContains(response.content, past_question.question_text, 
         status_code=200)