[Django] How Django outputs pages

Source: Internet
Author: User
1. Directly output -- httpresponse Helloworld. py

From Django. utils. httpwrappers Import Httpresponse
Def Index (request ):
  Return Httpresponse ( ''' <HTML> ''' );
Def Out (request ):
Response = Httpresponse (mimetype = ' Text/CSV ' )
Response [ ' Content-Disposition ' ] =   ' Attachment; filename1_1.txt '
Response. Write ( ' Abcdef ' )
  Return Response

URLs. py

Urlpatterns = Patterns ( '' ,
(R ' ^ $ ' , ' Test. helloworld. Index ' ),
)

2. MTC -- render_to_response

Helloworld. py (view)

# Coding = UTF-8
From Django. Core. Extensions Import Render_to_response
Def Index (request ):
Return Render_to_response ( ' Helloworld ' ,{ ' Params ' :{ ' A ' : 1 , ' B ' : 2 }})

Helloworld.html (Template)

< Html >
< Body >
< Table >
{% For key in Params %}
< Tr > < TD > {Key }} </ TD > < TD > {Params. Key }} </ TD > </ Tr >
{% Endfor %}
</ Table >
</ Body >
</ Html >

The above isOne pieceTo sum up, let me add another one. In fact, this is the first change.

3. Load from template

From Django. Template Import Loader, Context
T = Loader. get_template ( ' Common/post_note.htx ' )
C = Context ({ ' Action ' : Request. path, ' Title ' : Consts. add_topic })
Return Httpresponse (T. Render (c ))

 

Related Article

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.