The road to WeChat applet development (4) Django framework learning (list & dictionary)

The road to WeChat applet development (4) Application of Django framework learning template 3 In
runoob.html in templates, you can use the index subscript to retrieve the corresponding element

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>中国矿业大学计算机信息安全专业</title>
</head>
<body>
    <p>{
   
   { views_list }}</p>   # 取出整个列表
    <p>{
   
   { views_list.0 }}</p> # 取出列表的第一个元素
</body>
</html>

Modify the code in views.py

from django.shortcuts import render
from django.http import HttpResponse
 
def hello(request):
    return HttpResponse("Hello world ! ")
def runoob(request):
    views_list=["中国矿业大学","信息安全","29-1班"]
    return render(request, 'runoob.html', {
    
    "view_list",views_list})

Running the server
unexpectedly reported an error.
Insert picture description here
Look for the cause of the error.
Insert picture description here
It turns out that this should be a colon. In runoob.html in the
Insert picture description here
dictionary
templates, you can use the. Key to retrieve the corresponding value.

from django.shortcuts import render

def runoob(request):
    views_dict = {
    
    "name":"中国矿业大学"}
    return render(request, "runoob.html", {
    
    "views_dict": views_dict})
<p>{
   
   { views_dict }}</p>
<p>{
   
   { views_dict.name }}</p>

run
Insert picture description here

Guess you like

Origin blog.csdn.net/xulei1132562/article/details/113558229