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.
  • 相关阅读:
    浅析C#中的Thread ThreadPool Task和async/await
    C#中的集合类型
    WPF: StaticResource vs DynamicResource
    .NET程序员需要了解的概念、名词、术语--持续更新
    VisualStudio常见问题
    一些有用的.NET开源库--持续更新
    在MSBuild中使用Task实现自动引用指定版本的NuGet包
    C#与JMS的连接问题
    EventLog学习记录
    windows app设计原则总结-持续更新...
  • 原文地址:https://www.cnblogs.com/zxpo/p/3820407.html
Copyright © 2011-2022 走看看