Pycham developing Django project template tutorial

mb60090aff439bf 2021-10-28 22:18:06
pycham developing django project template

Templates

In previous chapters , The view function just returns the text directly , In fact, it is rarely used in the actual production environment , Because the actual pages are mostly styled HTML Code , This allows the browser to render very beautiful pages . At present, there are many template systems on the market , One of the most well-known and best used is DTL and Jinja2.​​DTL​​ yes ​​Django Template Language​​ Abbreviations of three words , That is to say Django The template language comes with . Of course, it can also be configured Django Support Jinja2 Wait for other template engines , But as a Django Built in template language , and Django It can achieve seamless connection without some incompatibilities . So I suggest that you study well DTL.

DTL With the ordinary HTML The difference between documents :

DTL A template is a special syntax HTML file , This HTML Documents can be Django compile , You can pass parameters in , Dynamic data . After compiling , Generate an ordinary HTML file , Then send it to the client .

Apply colours to a drawing template :

There are many ways to render a template . Here are two common ways .

  1. render_to_string: Find the template , Then compile the template and render it as Python String format of . Last pass HttpResponse Class is packaged as a HttpResponse Object back to . The sample code is as follows :
    from django.template.loader import render_to_string from django.http import HttpResponse def book_detail(request,book_id): html = render_to_string("detail.html") return HttpResponse(html)
  2. Although the above methods are very convenient . however django It also provides a simpler way , Directly render the template as a string and wrap it as HttpResponse The object is completed in one step . The sample code is as follows :
    from django.shortcuts import render def book_list(request): return render(request,'list.html')

Template lookup path configuration :

In the project ​​settings.py​​ In file . There is one ​​TEMPLATES​​ To configure , This configuration includes the configuration of the template engine , Configuration of template search path , Configuration of template context, etc . Template paths can be configured in two places .

  1. ​DIRS​​: This is a list , All template paths can be stored in this list , Use later in view ​​render​​ perhaps ​​render_to_string​​ When rendering a template , Will find the template in the path of this list .
  2. ​APP_DIRS​​: The default is ​​True​​, This is set to ​​True​​ after , Will be in ​​INSTALLED_APPS​​ Of installed ​​APP​​ Under the ​​templates​​ File plus find template .
  3. Lookup order : For example code ​​render('list.html')​​. First of all ​​DIRS​​ In this list, check whether there is this template under the path in turn , If there is , Just go back to . If ​​DIRS​​ None of the paths in the list were found , Then the current view will be checked first ​​app​​ Has it been installed? , If already installed , So first in the current ​​app​​ Under the ​​templates​​ Find template in folder , If not found , Then it will be installed in other ​​app​​ Search for . If no path is found , Then a ​​TemplateDoesNotExist​​ It's abnormal .


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

  1. Python代码阅读(第25篇):将多行字符串拆分成列表
  2. Quelles sont les fonctionnalités avancées de python?
  3. 运用python程序编写下面的的代码
  4. Python代码阅读(第25篇):将多行字符串拆分成列表
  5. 初学者 可以帮我看一下为什么Python程序运行不了吗
  6. 初學者 可以幫我看一下為什麼Python程序運行不了嗎
  7. Python代碼閱讀(第25篇):將多行字符串拆分成列錶
  8. Les débutants peuvent - ils m'aider à voir pourquoi le programme Python ne fonctionne pas?
  9. Lecture du Code Python (article 25): diviser les chaînes multilignes en listes
  10. Écrivez le code suivant en utilisant le programme Python
  11. Python exercises
  12. Python exercises
  13. Python exercises
  14. 随机试验数据函数统计分析python
  15. Python after class exercises (junior / October 11, 2021)
  16. python使用cv2.imread()读取图片失败
  17. python使用cv2.imread()讀取圖片失敗
  18. Python n'a pas lu l'image en utilisant cv2.imread ()
  19. Error debugging is accurate to lines, match case pattern matching... The official version of Python 3.10 is really friendly
  20. 100 basic Python interview questions Part II (41-60)
  21. Angry liver half moon! Python learning route + resource summary
  22. 如何用Python编写下列程序
  23. Comment écrire les programmes suivants en python
  24. 熬夜整理了2021年Python最新学习资料,分享给学弟学妹们【大学生必备】
  25. 朋友股票亏惨了,我一怒用Python爬取了证券最新数据
  26. Python爬虫高阶:微店混淆逆向解密
  27. Python爬虫开发学习全教程第二版,爆肝十万字【建议收藏】
  28. 我用Python逆向登录世界上最大的游戏平台,steam加密手段有多高明【内附源码】
  29. 我用Python爬取1000封情书助力室友表白班花,却反转再反转...原来这就是班花的终极秘密!
  30. 我用Python爬取了五千张美女图壁纸,每天一张忘记初恋!
  31. 我Python采集了新榜热门内容,原来这就是别人能成为自媒体大佬的秘密!
  32. 30个Python小游戏,上班摸鱼我能玩一天【内附源码】
  33. 【JS 逆向 AES逆向加密】Python爬虫实战,日子越来越有判头了
  34. python将两个列表进行合并,合并时删除重复元素
  35. J'a i utilisé Python pour accéder aux dernières données sur les titres.
  36. Rester debout tard pour trier les derniers documents d'apprentissage de Python 2021 et les partager avec les étudiants et les jeunes filles [Must for College Students]
  37. python中sklearn版本一直是0.0
  38. La version de sklearn en Python a toujours été 0.0
  39. django 自定义中间件如何忽略部分视图函数
  40. django 自定義中間件如何忽略部分視圖函數
  41. Comment les intergiciels personnalisés Django ignorent certaines fonctions de vue
  42. python map函数的使用
  43. python map函數的使用
  44. Utilisation de la fonction de carte Python
  45. 通过对于Arduino或是Python(包括相关库)的使用,结合自己的日常工作,描述相关知识在工作中起到了什么作用。
  46. 2021最全Python接口测试自动化教程【附学习资料】
  47. Python读取配置文件(config.ini),写入配置文件
  48. Python lit le profil (config.ini), écrit le profil
  49. python 两个数据列表匹配查找
  50. python 兩個數據列錶匹配查找
  51. Recherche de correspondance de deux listes de données Python
  52. python循环结构程序编写题目
  53. 【算法学习】1603. 设计停车系统(java / c / c++ / python / go / rust)
  54. Problèmes de programmation de la structure de la boucle Python
  55. 有人能用python写出代码吗wwww
  56. [apprentissage de l'algorithme] 1603. Conception du système de stationnement (Java / C / C + + / python / go / Rust)
  57. Python中if和else,总是显示无效语法是缩进问题吗?是的话可以详细讲解一下吗?
  58. Python的问题,希望有人帮帮忙
  59. Python的問題,希望有人幫幫忙
  60. Si et autrement en python, est - ce un problème d'indentation d'afficher toujours une syntaxe invalide? Oui, puis - je vous expliquer en détail?