Django Shortcut functions

Source: Internet
Author: User

  1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.