1. url配置
urlpatterns = [ re_path(r'temp_test/',temp_test) ]
2. views配置
def temp_test(request): name = "kyle" temp_list = [1,2,3,4] class Person(object): def __init__(self,name): self.name = name def eat(self): return "food" person_yuan = Person("yuan") person_tang = Person("tang") person_chan = Person("chan") person_list = [person_chan,person_yuan,person_tang] return render(request, "temp_test.html", {"name":name, "temp_list":temp_list, "person_list":person_list })
3. html获取变量
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p> {{ name }},别玩手机了</p> <p> {{ temp_list.1 }}</p> <hr> <p> {{ person_list.1.name }}</p> <p> {{ person_list.1.eat }}</p> </body> </html>
4. 页面测试