2012-09-24 53 views
0

所以我的網址是這樣的:顯示Django的URL列表

example.com?category=Home&category=Test 

我知道,在意見我可以讀這個名單是這樣的:

catlist = request.GET.getlist('category') 

但我怎麼能閱讀列表中的模板?

回答

1

從您的觀點來看,您需要將它傳遞給可以用作列表的模板。

例如

view.py

def myview(request): 
    catlist = request.GET.getlist('category') 
    #do something 
    .... 
    # pass catlist to template and may be some other variables 
    ctx = { 'catlist': catlist} 
    return render_to_response('mytemplate.html', ctx, 
       context_instance = RequestContext(request)) 

mytemplate.html

{% for cat in catlist %} 
    <p> {{cat}} </p> 
{%endfor%}