My Polls app question not displaying

29 views
Skip to first unread message

anyi.lloyd

unread,
Dec 14, 2017, 4:23:38 PM12/14/17
to Django users
Am new to Django and python. for the past 5 hours i have been battling to make my polls question to display in the index.html, but is not.
These are the codes

Model
from django.db import models

class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self):
return self.question_text

class Choice(models.Model):
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
question = models.ForeignKey(Question, on_delete=models.CASCADE)

def __str__(self):
return self.choice_text

views
from django.shortcuts import render
from django.http import HttpResponse
from .models import Question

def index(request):
latest_ques = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_ques}
return render(request, 'polls/index.html', context)

def detail(request, question_id):
return HttpResponse ("This a detail view of question: {}.".format(question_id))

def result(request, question_id):
return HttpResponse ("This a result view of question: {}.".format(question_id))

def vote(request, question_id):
return HttpResponse ("This a vote view of question: {}.".format(question_id))

Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Polls</title>
</head>
<body>
{% if latest_ques %}
<ul>
{% for question in latest_ques %}
<li><a href="/polls/{{ question.id }}/"><b>{{ Question.question_text }}</b></a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

</body>
</html>
 
Please need help guys. Thanks.

Matthew Pava

unread,
Dec 14, 2017, 4:27:22 PM12/14/17
to django...@googlegroups.com

You context dictionary key is “latest_question_list”, and this is passed into your template.  Your python variable is named latest_ques, which the template is not aware of.

So you need to change your html temple or your context key.

 

Also, watch your case.  “question” and “Question” are two different variables.

 

<!DOCTYPE html>
<
html lang="en">
<
head>
    <
meta charset="UTF-8">
    <
title>Polls</title>
</
head>
<
body>

{% if latest_question_list %}
    <
ul>
    {% for question in latest_question_list %}
        <
li><a href="/polls/{{ question.id }}/"><b>{{ question.question_text }}</b></a></li>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/44d61c17-1cac-4126-99ff-b6e0c1ca61c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

anyi....@gmail.com

unread,
Dec 14, 2017, 7:21:21 PM12/14/17
to django...@googlegroups.com
Thank u so much.

Sent from my HTC
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/02a08c6b3a154f8ab6883c7190a2d9a9%40ISS1.ISS.LOCAL.

AnyiLloyd

unread,
Dec 15, 2017, 5:51:16 AM12/15/17
to django...@googlegroups.com

Thank you so so much Mathew Pava. I have seen the problem.

Reply all
Reply to author
Forward
0 new messages