please im new to django.Need help understanding small code of django poll app.Highlighted with ******************

22 views
Skip to first unread message

Pachal Phillip

unread,
Apr 27, 2017, 9:21:40 AM4/27/17
to Django users
polls/views.py
from django.http import HttpResponse
from django.template import loader

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
  ************please Explain me this part specially ('latest_question_list'= latest_question_list) ******** context = {
        'latest_question_list': latest_question_list,
    }
    return HttpResponse(template.render(context, request))

Matthew Pava

unread,
Apr 27, 2017, 9:39:41 AM4/27/17
to django...@googlegroups.com

You are creating a dictionary with the name “context”.

A dictionary is initialized by surrounding key-value pairs in braces.  { }

The key and the value is separated by a colon. :

The pairs are separated from each other by a comma.  ,

Example:  dictionary = { key1: value1, key2: value2, key3: value3 }

 

 

Inside the context dictionary is one key of a string with the data ‘latest_question_list’.

That key is assigned the value of latest_question_list, which is a name that points to the value returned by Question.objects.order_by(‘-pub_date’)[:5].

 

template.render is a function that takes a context and a request.

request was an argument passed into the view function named index.

context is the dictionary with the value we assigned it above.

 

template.render returns a value that is passed to HttpResponse, which the view named index returns.

 

If you are new to programming, you may want to take a look at the Python tutorial:

https://docs.python.org/3.6/tutorial/

--
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/68a91c29-bac0-4189-8923-d46f3bc1ef87%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages