- 論壇徽章:
- 0
|
django中從views.py函數(shù)調(diào)用模板有下面幾種方法
1.render_to_response(template[, dictionary][, context_instance][, mimetype])
這個函數(shù)定義在django.shortcuts中。
template是模板名,要包含路徑的,這個路徑是加在django查找路徑之后的路徑;dictionary是傳遞給模板的模板變量值,鍵名是模板中的變量名,鍵值是模板中的變量值;context_instance主要是用來傳遞給模板RequestContext對象的(如果需要的話);mimetype是模板的MIME類型。
2.HttpResponseRedirect
這個類定義在django.http中。
這是一個HttpResponse的子類,使用他調(diào)用模板就相當(dāng)于return了一個HttpResponseRedirect對象,象這樣 return HttpResponseRedirect('***'),這里***是url路徑。如果***為/就指的是127.0.0.1,如果***為/abc/就指的是127.0.0.1/abc/。這種方法其實相當(dāng)于重定向了url,并沒有直接調(diào)用模板。
3.redirect(to[, permanent=False], *args, **kwargs)
這個函數(shù)是django1.1中新加的,他定義在django.shortcuts中。
這個函數(shù)函數(shù)的參數(shù)如果是一個model,那么這個model的get_absolute_url()函數(shù)將被調(diào)用;如果是一個view的名字,urlresolvers.reverse()將會被用來反向解析這個名字;如果是一個url,將會重定向到這個url。
4.也可以直接return一個HttpResponse對象。
HttpResponse也是定義在django.http中的。
直接返回一個HttpResponse對象的話,這個對象中應(yīng)該有模板和模板中的變量。用法如下:
t = loader.get_template('polls/index.html')
c = Context({
'latest_poll_list': latest_poll_list,
})
return HttpResponse(t.render(c))
5.generic views
django.views.generic.simple.direct_to_template
django.views.generic.simple.redirect_to
django.views.generic.list_detail.object_list
django.views.generic.list_detail.object_detail
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u3/103690/showart_2081810.html |
|