tags:

views:

41

answers:

3

I am sending list to a template using render_to_response. I am using django shortcuts. Hoe to do that? How to set context instance with a variable?

A: 

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's a nice documentation.

leoluk
A: 

You can send a list in a context. For instance:

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

Read the relevant documentation for more information.

If you want additional information to be passed to the template then use a RequestContext to wrap the context dictionary. You will need to enable the appropriate context processor for this.

Manoj Govindan
+1  A: 
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})

You can then access and enumerate list in the template like this (amongst other methods):

{% for i in mylist %}
   {{ i }}, 
{% endfor %}
Oli