zoukankan      html  css  js  c++  java
  • django--模板

    reference: http://www.djangobook.com/en/2.0/chapter04.html

    Those are the fundamentals of using the Django template system: just write a template string, create a Templateobject, create a Context, and call the render() method.

    • Any text surrounded by a pair of braces (e.g., {{ person_name }}) is a variable. This means “insert the value of the variable with the given name.” (How do we specify the values of the variables? We’ll get to that in a moment.)

    • Any text that’s surrounded by curly braces and percent signs (e.g., {% if ordered_warranty %}) is atemplate tag. The definition of a tag is quite broad: a tag just tells the template system to “do something.”

    • {{ ship_date|date:"F j, Y" }}, we’re passing the ship_date variable to the date filter, giving the date filter the argument "F j, Y". The date filter formats dates in a given format, as specified by that argument. Filters are attached using a pipe character (|), as a reference to Unix pipes.

    Here is the most basic way you can use Django’s template system in Python code:

    1. Create a Template object by providing the raw template code as a string.
    2. Call the render() method of the Template object with a given set of variables (the context). This returns a fully rendered template as a string, with all of the variables and template tags evaluated according to the context.

    In code, here’s what that looks like:

    >>> from django import template
    >>> t = template.Template('My name is {{ name }}.')
    >>> c = template.Context({'name': 'Adrian'})
    >>> print t.render(c)
    My name is Adrian.

    The manage.py shellcommand has one key difference: before starting the interpreter, it tells Django which settings file to use. Many parts of Django, including the template system, rely on your settings, and you won’t be able to use them unless the framework knows which settings to use.

    Rendering a Template

    Once you have a Template object, you can pass it data by giving it a context. A context is simply a set of template variable names and their associated values. A template uses this to populate its variables and evaluate its tags.

    
    

    A context is represented in Django by the Context class, which lives in the django.template module. Its constructor takes one optional argument: a dictionary mapping variable names to variable values. Call the Template object’srender() method with the context to “fill” the template:

    One thing we should point out here is that the return value of t.render(c) is a Unicode object – not a normal Python string.
  • 相关阅读:
    绑定对象
    类与对象
    视音频技术干货专栏
    sqlite3把字段为int32(用c++的time(nullptr)获取的)的秒数显示为年月日时分秒
    微信小程序开发 --- 小白之路 --- 心得
    spring cloud --- config 配置中心 [本地、git获取配置文件]
    spring cloud --- Zuul --- 心得
    spring boot --- 注解 @Bean 和@Component
    spring cloud --- Feign --- 心得
    spring cloud --- Ribbon 客户端负载均衡 + RestTemplate + Hystrix 熔断器 [服务保护] ---心得
  • 原文地址:https://www.cnblogs.com/zxpo/p/3820407.html
Copyright © 2011-2022 走看看