21 Jun 2020

render vs. HTTPResponse vs. HTTPResponseredirect

  • concepts
  • web
  • server-side
  • django
  • HTTP reponse code indicates a specific HTTP request has been successfully completed

    1. Informatioin responses(100-)
    2. Successful responses (200-)
    3. Redirects (300-)
    4. Client errors (400-)
    5. Server errors (400-)

    200 OK

  • GET: fetching and transmission of the source in the message body
  • HEAD: the entity headers are in the message body
  • PUT or POST: transmission of the resource describing the result in the message body
  • TRACE: the request message in the message body as received by the server
  • HTTPResponse()

    HTTP 200
  • returns a HTTPResponse object
  • usually used for small responses such as ajax dadta or small numbers

  • HTTPResponseredirect()

    HTTP 302 (found/moved temporarily)
  • redirects the page to the given url parameter

  • Render(request, template_name, context=None, content_type=None, stats-None, using=None)

  • instead of rendering the template in HTTPResponse()
  • combines the template with the context and returns HTTPResponse object

  • required

    request : object used to generate this response template_name

    optional

    context : a dictionary of values to add to the template context content_type : the MIME type to use fo rthe resulting doc.

    MIME types: is a media type that describes the nature and format of a document, file or assortment of bytes. type/subtype

    status: status code for the response using : the name of a template engine to use for loading the template

    Redirect(to, args, permanent=false, kwargs)

  • instead of calling reverse within HTTPResponseredirect(), call redirect to move to the given url
  • cannot transfer data between pages
  • use names from url pattern in urls.py