Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Django template system in Python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "sample analysis of the Django template system in Python", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and study the "sample analysis of Django template system in Python" this article.

Set template path

Create a templats file under the django project to store the html file

In order to reduce the redundant code of the template loading and calling process and the template itself, Django provides an easy-to-use and powerful API. When loading API using a template, you need to tell the framework the template path and set the template path in the project settings.py, as shown in the figure:

BASE_DIR in settings.py is the project path.

Set the template path in BIRS in TEMPLATES

Write index.html under templates and write the following code:

! DOCTYPE html > Home hello world!

Write the following code in the view file view.py to render the html file through render:

From django.shortcuts import render# gets the corresponding template and renders def index (request) through render: return render (request, 'index.html')

The results are as follows:

Template variable

{{}} is used in the Django template to represent variables:

{variable name}}: the variable name consists of alphanumeric and underscores, and its value can be any data type

Examples are as follows:

When the template engine encounters a variable, it calculates the variable and replaces it with the result

Home page {{content}} {{info}}

When render is rendered in view.py, the values are passed in a dictionary through context:

From django.shortcuts import renderdef index (request): content = 'hello world' info = {' name': 'test',' age': 18} return render (request, 'index.html', context= {' content': content, 'info': info})

The following syntax is supported in the template:

Home page {{content}} {{info.name}} {{li.1}} {{fun}} {{obj.name}}

View.py:

From django.shortcuts import renderdef index (request): content = 'hello world' info = {' name': 'test',' age': 18} li = [1,2,3] class Obj: def _ init__ (self, name): self.name = name M = Obj ('object attribute: MING') def fun (): return' method: fun' return render (request, 'index.html') Context= {'content':content,'info': info,'li': li,'fun': fun,'obj': M})

Referencing static files

First, create a directory under the root directory of the project to store static files, and set the path in settings, as follows:

STATIC_URL ='/ static/'

Is the static file reference prefix, which represents the file root directory when referencing the file, as follows:

Static stands for statics.

Home page

View.py:

From django.shortcuts import renderdef index (request): return render (request, 'index.html')

The above is all the content of the article "sample Analysis of Django template system in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report