DJANGO
Let's do a little bit more by fetching data from database and pass them to views in
Django
.For now we make a change in
polls/views.py
in index
function to get data from Question
model and return it as an HTTP response object. At first let's import the model:from .models import Question
And now in
index
function write the below code:latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)
The code above gets the last 5 questions and on the second line
question_text
fields for those questions are joined together by comma and finally returned as an HTTP response.
Now if you head over to
http://127.0.0.1:8000/polls/
you would see the result of question texts bound together by comma.
#python #django #query #django_part9