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].
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/44d61c17-1cac-4126-99ff-b6e0c1ca61c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.