Django (custom filters and custom labels)

Source: Internet
Author: User

    • A template is a Python string that has been tagged with the Django template language. Templates can contain template tags and variables.
    • A template tag is a marker that works in a template. For example, a template tag can produce the content of the control structure (if or for), get the database content, or access other template tags.
    • A tag block is surrounded by {percent}
    • Variable label surrounded by {{}}
    • The context is a key-value pair that is passed to the template.
    • Template rendering is to replace variables in the template and execute all template labels by getting values from the context.
Create a custom Label catalog

First create a templatetags directory under the app and add a few two. py files

extra_tags.py
The directory structure is as follows

Registering as an app in setting
Installed_apps = [    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'Blog',    'Mysites.templatetags', #作为app注册]

Add a custom filter in extratags.py

= template. Library () @register. Filter (Name='cut')  # namedef Mycut when used in a template (value, ARG):  # Replace the passed parameter arg with ' converted '    after conversion ')

Edit attempt function, pass value to template file

def  Index (Request):    'index.html', {'value' :'Hello Small Nine'})

Add URLs Route control

Urlpatterns = [    path ('admin/', admin.site.urls),    path ('  login/', views. Login ),    path ('index/', Views.index),]

Final start result display

Django (custom filters and custom labels)

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.