[Django] Development Daily _1.1_Day: Simple User Management System (2)

Template syntax: Write some placeholders in HTML, and replace and process these placeholders by data.

Table of contents

1. Create a new template

 2. Dictionary parameters -- basic data types & syntax

(1) Parameter: string

(2) Parameters: list

(3) Display list elements by index

(4) Syntax: loop

(5) Parameters: dictionary

(6) Parameters: list of dictionaries 

(7) Grammar: Conditional Statement

3. The basic process of common files of Django framework

4. Small case: fake Unicom News Center (climbing chong)

5. Finally, don't forget to synchronize the changed code to the code cloud


1. Create a new template

New html file:

tpl.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>学习模板语法</h1>
</body>
</html>

Write the views view function and URL path:

views.py:

from django.shortcuts import render,HttpResponse

# Create your views here.
def index(request):
    return HttpResponse("Hello Django!")

def user_list(requets):
    return render(requets,'user_list.html')

def user_add(requets):
    return render(requets,'user_add.html')

def tpl(requets):
    return render(requets,'tpl.html')

urls.py:

from django.urls import path
from app import views

urlpatterns = [
    path('index/', views.index),
    path('user/list/',views.user_list),
    path('user/add/',views.user_add),
    path('tpl/',views.tpl)
]

Open http://127.0.0.1:8000/tpl/ webpage:

 2. Dictionary parameters -- basic data types & syntax

(1) Parameter: string

views.py

def tpl(requets):
    name="代码骑士"
    return render(requets,'tpl.html',{"n1":name})

tpl.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>学习模板语法</h1>
<div>{
   
   { n1 }}</div>
</body>
</html>

Visit page:

 (2) Parameters: list

views.py

def tpl(requets):
    name="代码骑士"
    name_list=["小明","小红","李华","康康"]
    return render(requets,'tpl.html',{"n1":name,"n2":name_list})

tpl.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>学习模板语法</h1>
<div>{
   
   { n1 }}</div>
<div>{
   
   { n2 }}</div>
</body>
</html>

visit page

(3) Display list elements by index

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>学习模板语法</h1>
<div>{
   
   { n1 }}</div>
<li>{
   
   { n2.0 }}</li>
<li>{
   
   { n2.1 }}</li>
<li>{
   
   { n2.2 }}</li>
</body>
</html>

 Visit page:

(4) Syntax: loop

 tpl.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>学习模板语法</h1>
    <div>{
   
   { n1 }}</div>
    <div>
        {% for item in n2 %}
            <li>{
   
   { item }}</li>
        {% endfor %}
    </div>
</body>
</html>

visit website:

(5) Parameters: dictionary

views.py

def tpl(requets):
    name="代码骑士"
    name_list=["小明","小红","李华","康康"]
    role_dicts={"name":"小明","salary":100000,"position":"CEO"}
    return render(requets,'tpl.html',{"n1":name,"n2":name_list,"n3":role_dicts})

Different ways to handle dictionary values

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>学习模板语法</h1>
    <hr/>
    <div>{
   
   { n1 }}</div>
    <hr/>
    <div>
        {% for item in n2 %}
            <li>{
   
   { item }}</li>
        {% endfor %}
    </div>
    <hr/>
    <div>
        {
   
   { n3.name }}
        {
   
   { n3.salary }}
        {
   
   { n3.position }}
        <ul>
            {% for item in n3.keys %}
                <li>{
   
   { item }}</li>
            {% endfor %}
        <hr/>
            {% for item in n3.values %}
                <li>{
   
   { item }}</li>
            {% endfor %}
        <hr/>
            {% for item in n3.items %}
                <li>{
   
   { item }}</li>
            {% endfor %}
        <hr/>
            {% for k,v in n3.items %}
                <li>{
   
   { k }}={
   
   { v }}</li>
            {% endfor %}
        <hr/>
        </ul>
    </div>
</body>
</html>

Visit page:

(6) Parameters: list of dictionaries 

views.py

def tpl(requets):
    name="代码骑士"
    name_list=["小明","小红","李华","康康"]
    role_dicts={"name":"小明","salary":100000,"position":"CEO"}
    data_list=[
        {"name": "小明", "salary": 100000, "position": "CEO"},
        {"name": "小红", "salary": 100000, "position": "HR"},
        {"name": "康康", "salary": 100000, "position": "CTO"}
    ]
    return render(requets,'tpl.html',{"n1":name,"n2":name_list,"n3":role_dicts,"n4":data_list})

tpl.py

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>学习模板语法</h1>
    <hr/>
    <div>{
   
   { n1 }}</div>
    <hr/>
    <div>
        {% for item in n2 %}
            <li>{
   
   { item }}</li>
        {% endfor %}
    </div>
    <hr/>
    <div>
        {
   
   { n3.name }}
        {
   
   { n3.salary }}
        {
   
   { n3.position }}
        <ul>
            {% for item in n3.keys %}
                <li>{
   
   { item }}</li>
            {% endfor %}
        <hr/>
            {% for item in n3.values %}
                <li>{
   
   { item }}</li>
            {% endfor %}
        <hr/>
            {% for item in n3.items %}
                <li>{
   
   { item }}</li>
            {% endfor %}
        <hr/>
            {% for k,v in n3.items %}
                <li>{
   
   { k }}={
   
   { v }}</li>
            {% endfor %}
        <hr/>
        </ul>
    </div>
    <hr/>
    <div>
        {
   
   { n4.0 }}
        <br/>
        {
   
   { n4.0.name }}
        <br/>
        {
   
   { n4.0.salary }}
    </div>
    <hr/>
    <div>
        姓名|薪资
        {% for item in n4 %}
            <div>{
   
   { item.name }}|{
   
   { item.salary }}</div>
        {% endfor %}
    </div>
</body>
</html>

Visit page:

(7) Grammar: Conditional Statement

 tpl.html

<div>
        {% if n1 == "代码骑士" %}
            <h2>Hello!</h2>
        {% elif n1 == "代码超人" %}
            <h2>Hi!</h2>
        {% else %}
            <h3>你找错人了。</h3>
        {% endif %}
    </div>

Visit page:

3. The basic process of common files of Django framework

4. Small case: fake Unicom News Center (climbing chong)

Use crawler to get news webpage data: http://www.chinaunicom.com.cn/news/list202209.html

Get request URL: http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2022/09/news

 Write the view function in views.py

        *Note that the crawler message header needs to use a proxy, otherwise the json file cannot be crawled

def news(requet):
    #定义一个列表或字典存储数据
    #向网址:http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2022/09/news发送请求
    #使用第三方模块:requests
    import requests
    url = "http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2022/09/news"
    headers = {'User-Agent': 'Mozilla/4.0'}
    res = requests.get(url,headers=headers)
    data_list=res.json()
    print(data_list)
    return render(requet,'news.html',{"news_list":data_list})

 Write the urls.py file


    path('news/',views.news)

Write template news.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>联通新闻中心</title>
</head>
<body>
    <h1>联通新闻中心</h1>
    <div>
        <ul>
            {% for item in news_list %}
                <li>新闻:{
   
   { item.news_title }} 发布时间:{
   
   { item.post_time }}</li>
            {% endfor %}
        </ul>
    </div>
</body>
</html>

 A simple crawler page is ready:

5. Finally, don't forget to synchronize the changed code to the code cloud

Guess you like

Origin blog.csdn.net/qq_51701007/article/details/126789665