- 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)