03 template variables and template filters

03 template variables and template filters

1. Find template path

  • Find the order (two positions)

    1. Settings.py first looks in the TEMPLATES the 'DIRS'

    2. When the 'APP' = True INSTALLED_APPS list would go find the template in the app directory

  • Location of the template (two options)

    • When large projects, while centralized deployment, location of the template in the project root directory

    • When the app can be multiplexed to multiple projects using the template location in the app directory

  • Program 1:00, why build app folder under templates folder?

    • According to the search order to find a matching template returns

    • Therefore, in order to prevent the cover of the same name in different template app

       

2. template variables

  • Static pages, dynamic pages

    • Does not change with time, do not vary from client to change pages as static pages, there is no interaction

    • With the time change, change with the client, visitors to change the page to change the dynamic pages, there is an interaction

  • Template variable: in the render function, you can pass a variable that will render the value of the variable to the template (name can not start with an underscore)

    • In the render () was added a parameter context = {}, pass a dictionary key referenced in the template name, the value of the variable name

    • E.g. transfer context = { 'now': now} render functions in

    • Html may be referenced in the template {{now}}, now here it refers to the context of the 'now'

    • An app corresponding to one of the methods defined in the now variable

  • Parsing rules template variables

    • Calculated variables, replace it with the result

    • When it came to the point (.) And look for the following order

      1. Dictionary lookup keys

      2. Find a property or method

      3. Digital index lookup

    • If the result variable is callable (function), it is called with no parameters when calling becomes the value of the template

    • Failed to render, return null

3. The filter template filters operate on the template variables

  • Common Template variable filter syntax {{| filters: Parameter | Filters: Parameter | ······}},

    • add: The variable value is added first try integer add, may try other (string concatenation, list stitching) after a failure

    • capfirst capitalize the first letter, the first letter is not invalid value

    • date: the date formatted according to the format filling date 'is year Y MMM d m s seconds min H i'

      • Y: four-digit years like 1999

      • y: two-digit years, such as 99

      • m: a two-digit month, such as 01,09

      • n: number of months as a 1,9,12

      • d: two-digit date as 01,09,31

      • j: Day as a number of 1,9,31

      • g: The number of 12-hour of one hour as 1, 9, 12

      • G: The number of hours of a 24-hour clock, such as 0, 8, 23

      • h: digit of hours, such as 12-hour 01,09,12

      • H: digit of hours, such as 24-hour 01,08,23

      • i: 00-59 minutes

      • s: 00-59 seconds

    • time time formatting, in the format fill time, only the minutes and seconds

    • default: When variable rendering fails, the return of default parameters Note: When a variable is returned already '', also returns parameter

    • first returns the first element of the list

    • last returns a list of the last element

    • slice: '::' returns a list of sections

    • join: '' string concatenation

    • floatformat: floating-point decimal places format, do not specify the default to one decimal place

    • length returns the length of the string or list

    • length_is: number returns a Boolean value, whether the length of the list for the number

    • lower strings all lowercase

    • All upper string uppercase

    • All title separated by a space d capitalize the first letter of the word

    • Close safe engine automatically escaping template variable (html, css, js), such that the label is valid

  • xss (cross-domain scripting attacks)

    • In order to prevent variable web pages cross-domain scripting attacks as a markup language (html, css, js), django template engine default variable for these contents, when rendering the tag <> escape back to the & lt, & gt, prevent its template Have an effect

    • Use safe filter, a variable can be trusted by the safe filter, unescaping template engine

4. static files (css, js, images)

  1. Path Configuration

    • STATICFILES_DIRS = [os.path.join(BASE_DIR,static)]
    • STATIC_URL

  2. Import static files

    1. Hard-coded by STATIC_URL

    2. Dynamic template tags introduced at this time will not affect whether STATIC_URL Why can hide path

      1. Beginning html document load static folder: {% load static%}

      2. href link tag = "{% static 'relative path'%} '

Guess you like

Origin www.cnblogs.com/zonexxlcode/p/11388901.html