2011-05-09 92 views
0

我的圖像是根據配置文件參數命名的。將信息傳遞給django模板圖像

一個例子是 - 個人資料編號3423

圖像將HTTP /路/ imagebig3423.jpg

<img alt="" src="http://path/imagebig[profileid]" /> 

我如何傳遞信息給模板?

+0

這聽起來真的就像對象層中的設計缺陷一樣。您的個人資料沒有影像字段是否有原因? – markijbema 2011-05-09 19:41:23

+0

這不是個人資料圖片。這是一個基於註冊詳細信息自動生成的圖像。 – Eva611 2011-05-09 19:54:24

回答

1

views.py:

def some_view(request): 
    # get the desire profile_id to pass to the template or set explicitly 
    profile_id='3423' 
    context = { 'profile_id':profile_id, } 
    return render_to_response('some_template.html', context, context_instance=RequestContext(request)) 

some_template.html:

<img alt="" src="http://path/imagebig{{ profile_id }}" /> 
1

通常你傳遞一個哈希模板,你可以把你的模板在

首先,你可能會發現它涵蓋getting started with templates教程的價值重新閱讀第3部分。再來看一下模板的文檔in detail

剪斷,會是這樣的一個代碼:

from django.shortcuts import render_to_response, get_object_or_404 
def detail(request, profile_id): 
    p = get_object_or_404(Profile, pk=profile_id) 
    return render_to_response('profile/detail.html', {'profile': p})