在Django模板中定义变量

时间:2018-09-30 10:49:10

标签: django loops templates for-loop

我有字典和清单。字典包含youtube视频的标题和链接,列表包含描述。我要打印相应的图块和说明。

{% for key,values in link.items%}
    <a><h4>{{key}}</h4></a>
    <p><b>Description:</b> {{description[how do i iterate the description]}}</p>
{%endfor%}

我试图创建一个变量{{i}}并对其进行递增,但这没有用。

视图。

def search(request):
    from . import search_yt 
    #list for thumbnail src
    thumb = []
    #link is dict and description is list
    link,description = search_yt.search_youtube(request.POST['query'])

    #Preparing the thumbnail src
    for key,value in link.items():
        key = key
        thumb.append("http://img.youtube.com/vi/"+value[32:]+"/hqdefault.jpg")

    context = {
        'query' : request.POST['query'],
        'link' : link,
        'description' : description,
        'thumbnail' : thumb
    }
    return render(request,'index/search.html',context)

def search(request): from . import search_yt #list for thumbnail src thumb = [] #link is dict and description is list link,description = search_yt.search_youtube(request.POST['query']) #Preparing the thumbnail src for key,value in link.items(): key = key thumb.append("http://img.youtube.com/vi/"+value[32:]+"/hqdefault.jpg") context = { 'query' : request.POST['query'], 'link' : link, 'description' : description, 'thumbnail' : thumb } return render(request,'index/search.html',context)

0 个答案:

没有答案