Django框架模板语法之深度查询!!!

 

 

 

 

 

"""mysite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

#form app01 import views as v1
#from app02 import views as v2

from app01.views import get_timer
from app02.views import login,index
from app03.views import muban
urlpatterns = [
path('admin/', admin.site.urls),
path("timer", get_timer),
path("index", index),
path("login/", login),
path("login", index),
path("muban/", muban),
]

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>模板语法</h2>
<h3>变量渲染之深度查询</h3>
<p>姓名: {{ name }}</p>
<p>四大名著: {{ books }}</p>
<p>四大名著第三本: {{ books.2 }}</p>
<p>个人信息: {{ info }}</p>
<p>个人信息 姓名: {{ info.name }}</p>
<p>个人信息 年龄: {{ info.age }}</p>
<p>stus中第三个学生的姓名:{{ stus.2.name }}</p>

<h3>过滤器</h3>
{#{{val|过滤器函数:参数}}#}
<p>当前时间:{{now|date:"Y-m-d"}}</p>
<p>文件大小:{{fileSize|filesizeformat}}</p>


</body>
</html>

 

from django.shortcuts import render,redirect
from django.shortcuts import HttpResponse
# Create your views here.
class Student(object):
def __init__(self,name,age):
self.name = name
self.age = age
def muban(request):
name = "huchangxi"
books = ["西游记","三国演义","水浒传","红楼梦"]
info = {"name":"rain","age":23}
#return HttpResponse("用户名或密码验证失败")

s1 = Student("huchangxi", 22)
s2 = Student("hulanxi", 23)
s3 = Student("changxi", 32)
stus = [s1, s2, s3]
import datetime
now = datetime.datetime.now()
fileSize = 42342432
return render(request, "muban.html", {"name": name, "books": books, "info": info,"stus":stus,"now":now,"fileSize":fileSize})

 

posted @ 2022-06-30 19:54  呼长喜  阅读(34)  评论(0编辑  收藏  举报