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>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
</body>
</html>
From: [email protected] [mailto:[email protected]] On
Behalf Of anyi.lloyd
Sent: Thursday, December 14, 2017 10:56 AM
To: Django users
Subject: My Polls app question not displaying
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.
--
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
[email protected]<mailto:[email protected]>.
To post to this group, send email to
[email protected]<mailto:[email protected]>.
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<https://groups.google.com/d/msgid/django-users/44d61c17-1cac-4126-99ff-b6e0c1ca61c4%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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 [email protected].
To post to this group, send email to [email protected].
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/02a08c6b3a154f8ab6883c7190a2d9a9%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.