render httprequest

render
def my_view(request):# View code here...
t = loader.get_template('myapp/index.html')
c = RequestContext(request, {'foo': 'bar'})
return HttpResponse(t.render(c),content_type="application/xhtml+xml")

這就是使用 Django 模板系統的基本規則: 寫模板,建立 Template 對象,建立 Context , 調用 render() 方法html

 
 
 return render(request, "message.html", {"message": u"待辦事項添加失敗"})
 
 返回到render中指定的message.html頁面 而且以字典的方式傳值到該模板中好比:{"message": u"待辦事項添加失敗"}
 
 
redirect
return redirect(resolve_url( 'index' ))能夠經過redirect直接進行跳轉,不過寫的是絕對路徑,因此這種相對要好一點

 

return redirect('/index/') 效果一樣

HttpRequest

返回參數給前端 {"message": u"待辦事項添加失敗"}

 

Django之HttpRequest和HttpResponse

 
Django之HttpRequest和HttpResponse - songyalong1117 - 宋亞龍


render

Combines a given template with a given context dictionary and returns an  HttpResponse  object with that rendered text.
我認爲是返回渲染過的html頁面和一些變量,還有HttpResponse object
 

render_to_response

Renders a given template with a given context dictionary and returns an HttpResponse object with that rendered text.
返回html頁面和HttpResponse object
 
 
  1. response = HttpResponse("Here's the text of the Web page."):
    will create a new HttpResponse object with HTTP code 200 (OK), and the content passed to the constructor. In general, you should only use this for really small responses (like an AJAX form return value, if its really simple - just a number or so).前端

  2. HttpResponseRedirect("http://example.com/"):
    will create a new HttpResponse object with HTTP code 302 (Found/Moved temporarily). This should be used only to redirect to another pageweb

    Renders a given template with a given context dictionary and returns an HttpResponse object with that rendered text.django

 
 經過 
 
httpresponse render_to_response與render返回數據給前端頁面,通常返回html(模板)給前端,也能夠返回其餘數據




 



相關文章
相關標籤/搜索