其他分享
首页 > 其他分享> > Django框架模板语法之深度查询!!!

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>
</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("用户名或密码验证失败")
return render(request,"muban.html",{"name":name,"books": books,"info": info})
s1 = Student("huchangxi", 22)
s2 = Student("hulanxi", 23)
s3 = Student("changxi", 32)

 

标签:info,name,views,Django,语法,urls,path,import,模板
来源: https://www.cnblogs.com/A121/p/16428308.html