Pycham develops template tags commonly used in Django project

mb60090aff439bf 2021-10-29 19:23:39
pycham develops template tags commonly

Common template labels :
  1. if label :if The label is equivalent to Python Medium if sentence , Yes elif and else Corresponding , But all labels need label symbols ({%%}) Package .if You can use the ==、!=、<、<=、>、>=、in、not in、is、is not Equal judgment operator . The sample code is as follows :
    {% if " Zhang San " in persons %} <p> Zhang San </p> {% else %} <p> Li Si </p> {% endif %}
  2. for...in... label :for...in... Be similar to Python Medium for...in.... You can traverse the list 、 Tuples 、 character string 、 Dictionaries and all other objects that can be traversed . The sample code is as follows : {% for person in persons %} <p>{{ person.name }}</p> {% endfor %} If you want to traverse backwards , Then add a... When traversing reversed. The sample code is as follows : {% for person in persons reversed %} <p>{{ person.name }}</p> {% endfor %} When traversing the dictionary , Need to use itemskeys and values Other methods . stay DTL in , You cannot execute a method in parentheses . The sample code of traversal dictionary is as follows : {% for key,value in person.items %} <p>key:{{ key }}</p> <p>value:{{ value }}</p> {% endfor %} stay for In circulation ,DTL Some variables are available . These variables are as follows :
  • ​forloop.counter​​: The subscript of the current loop . With 1 As the starting value .
  • ​forloop.counter0​​: The subscript of the current loop . With 0 As the starting value .
  • ​forloop.revcounter​​: The reverse subscript value of the current loop . For example, the list has 5 Elements , So the first time I traverse this property is equal to 5, The second, 4, And so on . And it is based on 1 As the subscript of the last element .
  • ​forloop.revcounter0​​: Be similar to forloop.revcounter. The difference is that the subscript of the last element is from 0 Start .
  • ​forloop.first​​: Is it the first time to traverse .
  • ​forloop.last​​: Is it the last traversal .
  • ​forloop.parentloop​​: If there are multiple loops nested , So this property represents the upper level for loop .
  1. for...in...empty label : This label uses the following for...in... It's the same , Just in the case of traversing objects without elements , Will execute empty The content in . The sample code is as follows :
    {% for person in persons %} <li>{{ person }}</li> {% empty %} There is no one yet {% endfor %}
  2. with label : Define variables in the template . Sometimes it's more complicated to access a variable , You can cache this complex variable to a variable first , You can use this variable directly in the future . The sample code is as follows : context = { "persons": [" Zhang San "," Li Si "] } {% with lisi=persons.1 %} <p>{{ lisi }}</p> {% endwith %} There are a few points that need intense attention :
  • stay ​​with​​ A variable defined in a statement , Only in ​​{%with%}{%endwith%}​​ Use in , It can't be used outside this label .
  • When defining variables , You can't leave spaces around the equal sign . such as ​​{% with lisi = persons.1%}​​ It's wrong. .
  • There is another way of writing that is also supported : {% with persons.1 as lisi %} <p>{{ lisi }}</p> {% endwith %}
  1. url label : In the template , We often have to write something url, For example, a certain a The label needs to define href attribute . Of course, if you hardcode this directly url It's OK to write dead in it . But this may not be a good thing for future project maintenance . Therefore, it is recommended to use this inversion method to achieve , Be similar to django Medium reverse equally . The sample code is as follows :
    <a href="{% url 'book:list' %}"> Book list page </a>
    If url Parameters need to be passed when reversing , Then it can be passed on later . But parameters are divided into position parameters and keyword parameters . Position parameter and keyword parameter cannot be used at the same time . The sample code is as follows :
    # path part path('detail/<book_id>/',views.book_detail,name='detail') # url reverse , Use position parameters <a href="{% url 'book:detail' 1 %}"> Book details page </a> # url reverse , Use keyword parameters <a href="{% url 'book:detail' book_id=1 %}"> Book details page </a>
    If you want to use url When the tag is reversed, the parameters of the query string should be passed , Then you have to manually add . The sample code is as follows :
    <a href="{% url 'book:detail' book_id=1 %}?page=1"> Book details page </a>
    If you need to pass more than one parameter , Then separate them by spaces . The sample code is as follows :
    <a href="{% url 'book:detail' book_id=1 page=2 %}"> Book details page </a>
  2. spaceless label : remove html White space characters in labels . Including Spaces 、tab key 、 Line break, etc . The sample code is as follows :
    {% spaceless %} <p> <a href="foo/">Foo</a> </p> {% endspaceless %}
    So after rendering , It will become the following code :
    <p><a href="foo/">Foo</a></p>
    spaceless It will only be removed html White space between labels . The white space between the label and the text is not removed . Look at the following code :
    {% spaceless %} <strong> Hello </strong> {% endspaceless %}
    This will not be removed strong White space characters in .
  3. autoescape label : Turn on and off the automatic escape function of elements in this tag . Automatic escape is the ability to escape some special characters . such as < Escape into html A character recognized by grammar , Will be taken as &lt;, and > Will be automatically escaped to &gt;. In the template, automatic escape has been turned on by default .autoescape The sample code of is as follows :
    # Context information passed context = { "info":"<a href='www.baidu.com'> Baidu </a>" } # Turn off auto escape in template {% autoescape off %} {{ info }} {% endautoescape %}
    Then a hyperlink of Baidu will be displayed . If you put off Change to on, Then it will be displayed as an ordinary string . The sample code is as follows :
    {% autoescape on %} {{ info }} {% endautoescape %}
  4. verbatim label : Default in DTL The template will parse those special characters . such as {% and %} as well as {{ etc. . If you don't want to use... In a piece of code DTL Parsing engine . So you can put this snippet in verbatim In the label . Under the sample code :
    {% verbatim %} {{ if dying}}Still alive.{{/if}} {% endverbatim %}


版权声明
本文为[mb60090aff439bf]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/10/20211014111844647B.html

  1. 请问这个实验如何做(用python3.x的环境)
  2. [algorithm learning] 1486. Array XOR operation (Java / C / C + + / Python / go / trust)
  3. Comment créer un programme Python pour simuler la réservation de sièges dans un cinéma
  4. Comment faire cette expérience (en utilisant l'environnement Python 3.x)
  5. Python之绘制多角星
  6. Pandas的SettingWithCopyWarning报警怎么解决
  7. Comment résoudre l'alarme de réglage avec copywarning de pandas
  8. 使用 Python 进行数据可视化之Matplotlib
  9. Python 常见问题 - 使用 poetry build 打包构建失败,报 ModuleOrPackageNotFound No file/folder found for package
  10. 万圣节奇妙夜,不给糖就捣蛋—Python带你点击解锁恐怖新道具(南瓜头、骷髅、蝙蝠、糖......)
  11. 【算法学习】1221. 分割平衡字符串(java / c / c++ / python / go / rust)
  12. 教你用Python 编写 Hadoop MapReduce 程序
  13. Je t'ai appris à écrire Hadoop mapreduce en Python.
  14. Pandas下载最新版和python不匹配
  15. python tkinter GUI编程如何实现一个时钟,无论主线程怎么卡住,都不影响时钟正常进行走时间。
  16. python tkinter GUI編程如何實現一個時鐘,無論主線程怎麼卡住,都不影響時鐘正常進行走時間。
  17. La programmation de l'interface graphique Python tkinter pour implémenter une horloge n'affecte pas le temps de fonctionnement normal de l'horloge, peu importe comment le fil principal est coincé.
  18. Pandas télécharger la dernière version ne correspond pas à Python
  19. Make a cool music player with Python [V3.0 including source code and packaged exe]
  20. 编写一个函数,可以传入两个实参或三个实参。如果传入两个实参,返回求和。如果传入三个实参,返回的话。前两个实参的和对第三个实参求余的结果Python
  21. python用tcp写server传输文件遇到的问题
  22. Python - - fichiers et exceptions
  23. Python failed to install pyinstaller, preparing wheel metadata... Error
  24. Python Turtle库 画魔法少女里的小圆晓美焰
  25. Quels sont les types de données intégrés de python? Les chiffres.
  26. python代碼實現掃碼關注公眾號登錄
  27. Mise en œuvre du Code Python
  28. 捋一捋Python中的数学运算math库(上篇)
  29. Python:晚上把附近的足浴店都给爬了一遍,好兄弟真不错
  30. python接收字符串,返回其中字符的最后一次出现,并顺序存入列表
  31. python post请求特殊data数据怎么发送
  32. Python: J'ai escaladé tous les salons de foot voisins la nuit. C'est un bon frère.
  33. Retouchez la Bibliothèque math ématiques en Python (Partie 1)
  34. 拓端tecdat:Python 用ARIMA、GARCH模型预测分析股票市场收益率时间序列
  35. 拓端tecdat|PYTHON贝叶斯推断计算:用BETA先验分布推断概率和可视化案例
  36. Extension tedat | Python Bayesian Inference Calculation: Inference Probability and Visualization Case Using Beta precedent distribution
  37. python字典里的键是不可变类型,值是可变类型对不对
  38. Extension tedat: Python Prediction and Analysis of Stock Market return time series Using Arima and GARCH Models
  39. Python小白福音!Github开源了一款神器....
  40. Python LITTLE WHITE Gospel! Github a ouvert un artefact...
  41. Opencv Python method for adjusting image contrast and background bleaching of white paper photos with text
  42. 利用Python算法画出美丽动人的妹子图像
  43. 利用Python算法畫出美麗動人的妹子圖像
  44. Utiliser l'algorithme Python pour dessiner de belles images de filles
  45. 关于python程序编写 查找数据
  46. À propos de l'écriture de programmes Python pour trouver des données
  47. python turtle 绘制七段数码管以及14段数码管显示字母和时间
  48. La tortue Python dessine sept segments de tubes numériques et 14 segments de tubes numériques pour afficher les lettres et l'heure
  49. What job can I find after learning Python? Are you ready for employment now?
  50. 关于#python#的问题,请各位专家解答!
  51. 關於#python#的問題,請各比特專家解答!
  52. S'il vous plaît répondre aux questions de Python!
  53. python代码出错,如何修改
  54. python代碼出錯,如何修改
  55. Erreur de code Python, comment modifier
  56. 如何使用Python进行超参调参和调优
  57. Comment utiliser Python pour le réglage hyperparamétrique
  58. 关于#python#的问题,请各位专家解答!
  59. S'il vous plaît répondre aux questions de Python!
  60. Quick start of automation -- python (2) - [string and operator] - half an hour a day