Iv. Python Django Template variables

Source: Internet
Author: User

Python Django Template variables

One, passing string variables


# Cat Blog/template/index.html

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>

<title>{{title}}</title>

<body>

</body>


# Cat blog/views.py

From django.shortcuts import Render_to_response

def index (req):

Return Render_to_response (' index.html ', {' title ': ' Loyu page ', ' User ': ' Loyu '})


Second, pass dictionary, list variable

# Cat Blog/template/index.html


<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>

<title>{{title}}</title>

<body>

<p>hello: {{user.age}}</p>

<p>hello: {{user.sex}}</p>

<p>{{book_list.0}}</p>

<p>{{book_list.1}}</p>

<p>{{book_list.2}}</p>

<p>{{book_list.3}}</p>

</body>



# Cat blog/views.py


From django.shortcuts import Render_to_response


def index (req):

user = {' name ': ' Loyu ', ' age ': $, ' sex ': ' Male '}

Book_list = [' Python ', ' java ', ' php ', ' web ']

Return Render_to_response (' index.html ', {' title ': ' Loyu page ', ' user ': User, ' Book_list ': book_list})


Third, transfer class variables


# Cat Blog/template/index.html


<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/>

<title>{{title}}</title>

<body>

</body>


# Cat blog/views.py


From django.shortcuts import Render_to_response

class Person (object):

def __init__ (self, name, age, Sex):

Self.name = Name

Self.age = Age

Self.sex = Sex

def index (req):

user = person (' Loyu ', ' Male ')

Return Render_to_response (' index.html ', {' title ': ' Loyu page ', ' User ': User})


This article is from "Meteor Yu" blog, please be sure to keep this source http://8789878.blog.51cto.com/8779878/1850742

Iv. Python Django Template variables

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.