Sometimes the data returned by the backend, front-end processing needs, such as more than XX characters show ... this demand, we need to use the filter. django has built-in filter module, or you can define your own filter to filter.
A, django comes with filter:
views.py:
DEF Test (Request): Import datetime CUR_TIME = datetime.datetime.now () Age = 18 is name = ' Marry ' article_content = ' Django has built-in tab, very easy to use, as follows: django has built-in tab, very easy to use, the code is as follows: django has built-in paging, very easy to use, as follows: ' navs = [ " my diary " , " my album " , ' I feel ' , ' I feel 1 ' , ' I feel 2 ' , ' my mood 3 ' ] comments = " <H1 of style = 'font-size: 500px'> you okay </ h1 of> " h1_str = ' <P style = "font-size: 98px"> ha ha </ P> ' js_str = " <Script> alert ( 'ha ha ha') </ Script> " article_content = " North Korea a gold and three fat brother, had boarded the sun, in order to avoid the hot sun, selection Ben evening sun " say = " today dinner, hit a silly x, the guy is too 213, and openly to jump the queue, after dinner defecating and urinating in the street, enough of sb. " return the render (Request, ' test.html ' , about locals ()) # about locals () is to all defined variables return
Second, the custom filter: build directory is a directory tags are put under app module. my_tags.html
Directory Structure:
Code:
from Django Import Template # custom tags. You must create a python package in the user, and the package name must be "templatetags" the Register = template.Library () # the Register the name is fixed @ register.filter (name = ' HM ' ) # name here is when the front-end method invocation the name, without words is the method name #@register.filter () DEF Mingan (value): IF " gold and three fat " in value: value = value.replace ( " gold and three fat " , " Kim Jong-un " ) return value @ register.filter () DEFmingan2 (value, S): # front end parameter passing, a maximum of two custom filter parameters IF " golden three fat " in value: value = value.replace ( " gold three fat " , S) return value # Simple Tag: register.simple_tag @ # the simple_tag can pass a plurality of parameters DEF mingan3 (value, * args): for S in args: IF S in value: value = value.replace (S, " ** " ) return value
Third, the front end comes with a custom filter and filter way
{% Load my_tags%} # here to introduce custom filter {# introduced custom Tags #} <! DOCTYPE HTML > < HTML lang = "EN" > < head > < Meta charset = "UTF-. 8" > < title > the title </ title > </ head > < body > {{Age}} {{Age | the add: "1"}} {# 1 added on the basis of #age} < br > {name}} { {{name | add: "sir" | Upper}} {#} supports chaining # < br > {{ comments }} {#{{ comments|safe }}#} {# If added back safe, then, that this is a safe string, the browser parses according to the label content. CSS injection is likely to cause, when the browser does not need to parse the label, do not add #} Safe < br > {{article_content | truncatechars: "20"}} {# truncates the string, the extra string ... display} # < br > {{navs | slice: "0:. 3"}} {#} slice # < br > {navs.0} {} {# obtaining 0-th element} # < br > {{navs | }} length {#} lENGTH # < br > {{navs | the Join: "===="}} {#} splicing # < br > {{author | default: "administrator"}} < br > { CUR_TIME {| DATE: 'the Y / m / D H: I: S'}} < Js_str # {{{| safe}} #} {# if added back safe, then, that this is a safe string, the browser parses according to the label content. Likely to cause injection JS #} < br > {{article_content | HM}} {{article_content | mingan2: 'Gold fat'}} {# #} register.filter Usage {#} Usage # < br > {% mingan3 say "213 "" sb "" silly X "%} {} #simple Tag usage # < br > {{name | Lower}} </ body > </ HTML >
Four, urls.py urlpatterns add the "path ( 'test /', views.test),"
Then run django project, visit 127.0.0.1:8000/test. It should look Style