Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
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 que
stions 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 co
mma.

#python #django #query #django_part9