- The Django.shortcuts package provides helper classes and functions that make it easier to manipulate every part of MVC, including:
- Render(request, Template_name,[dictionary],[context_instance],[content_type],[status],[current_app],[dirs]): Returns the HttpResponse object with the given context fill template, which is the content of the template after it is replaced. Render () function with Context_instance (RequestContext) The Render_to_response function is the same for the parameter invocation
render #如下代码功能相同from django.shortcuts import renderdef My_ View (Request): # View Code here ... return render (Request, ' myapp/index.html ', {"foo : "bar "}, content_type= "application/ Xhtml+xml ") from django.http import httpresponsefrom django.template Span style= "COLOR: #0000ff" >import RequestContext, loaderdef 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 ") /pre>
- Render_to_response(template_name[, dictionary][, context_instance][, content_type][, dirs]): Fills the template with the given context and returns HttpResponse object,
Render_to_responsereturnRender_to_response (' my_template.html ', My_data_dictionary, Context_insta Nce=requestcontext (Request)) #如下代码功能相同from Django.shortcutsImportRender_to_responsedef My_view (Request): # View Code here ...returnRender_to_response (' myapp/index.html ', {"Foo": "Bar"}, Content_type="Application/xhtml+xml") from Django.httpImportHttpresponsefrom django.templateImportContext, Loaderdef My_view (Request): # View Code here ... t = loader.get_template (' myapp/index.html ') c = Context ( {' foo ': ' Bar '})returnHttpResponse (T.render (c), content_type= "Application/xhtml+xml")
- redirect (to, [permanent=false],*args, **kwargs): Returns the httpresponseredirect corresponding to the URL of the to object, The default is temporary redirection, and if Permanent=true is permanently redirected, the parameters to can be:
- A model: the Get_absolute_url () function of this model is called
- A view name:urlresolvers.reverse will be called Reverse parse view name to get URL
- An absolute or relative URL
- get_object_or_404 (Klass, *args, **kwargs): Calls the Get () function with the given model manager, throws a Http404 exception if not obtained
- get_list_or_404 (Klass, *args, **kwargs): Invokes the filter function with the given model manager to return the result to the list, and throws a Http404 exception if the list is empty
Django Shortcut functions