How to give an answer?

I am posting a list to a template using render_to_response. I am using django shortcuts. Can you do it? How to set a context instance with a variable?

+4
source share
3 answers
from django.shortcuts import render_to_response def my_view(request): mylist = ['item 1', 'item 2', 'item 3'] return render_to_response('template.html', {'mylist':mylist}) 

Then you can access and list in a template like this (among other methods):

 {% for i in mylist %} {{ i }}, {% endfor %} 
+2
source

Like any template value.

 def some_view(request): # ... my_data_dictionary = { 'somelist': my_list } return render_to_response('my_template.html', my_data_dictionary, context_instance=RequestContext(request)) 

By the way, there is good documentation .

+2
source

You can send the list in context. For instance:

 my_list = [1, 2, 3, 4] context = dict(my_list = my_list) render_to_response(template, context) 

Read more for more information.

If you want additional information to be passed to the template, use RequestContext to wrap the context dictionary. To do this, you need to enable the appropriate context processor.

0
source

All Articles