Reputation: 180
In my browser, everything is fine. Until I make a test
here is my polls/views.py
from django.shortcuts import render
from polls.models import Poll
def index(request):
latest_poll_list = Poll.objects.all().order_by('-pub_date')
context = {'latest_poll_list':latest_poll_list}
return render(request,'polls/index.html',context)
polls/templates/polls/index.html
{% if latest_poll_list %}
<ul>
{% for poll in latest_poll_list %}
<li>{{poll.question}}</li>
{% endfor %}
</ul>
{% else %}
<p>No Poll Available</p>
{% endif %}
and my polls/tests.py
from django.test import TestCase
from django.core.urlresolvers import reverse
class SimpleTest(TestCase):
def test_this(self):
response = self.client.get(reverse('polls.views.index'))
print response.context
print response.content
as you can see, my response.context['latest_poll_list']
is always []
So I wonder where is my fault?
Upvotes: 2
Views: 869
Reputation: 19578
If in the browser you get your objects this means that your view is ok, if your test does not return any object maybe you have to create them (tests make use of an empty database automatically created from the scratch by Django). I usually create sample objects in setUp() method:
class SimpleTest(TestCase):
def setUp(self):
self.poll = Poll.objects.create()
Upvotes: 2