【问题标题】:what is the use of room in the in the render section?渲染部分中的房间有什么用?
【发布时间】:2022-01-25 16:20:17
【问题描述】:

***python Django 框架 我不明白渲染部分中 {'rooms':rooms}) 的用途是什么,请帮助我刚刚学习 django


import imp
from django.shortcuts import render
from django.http import HttpResponse


rooms =[
    {'id':1, 'name':'learn python'},
    {'id':2, 'name':'learn html and javascript'},
    {'id':3, 'name':'at last learn django'},
]



# Create your views here.
def home(request):
    return render(request,'home.html',
                  **{'rooms':rooms})**


def room(request):
    return render(request,
                    'room.html') 

【问题讨论】:

标签: django django-models django-views django-forms django-templates


【解决方案1】:

{'rooms':rooms} 是具有键/值对的字典。

您将此键/值对发送到 home.html 并带有此语句

return render(request,'home.html',
                  {'rooms':rooms})

现在在 home.html 中,您可以使用键 rooms 访问该字典。例如,

home.html

<html>
  <head>
  </head>
  <body>
    {{ rooms }}
  </body>
</html>

键和值不需要同名。除了{'rooms':rooms},您还可以使用{'number_of_rooms':rooms}。但是您必须使用键 number_of_rooms

访问 home.html 中的值

home.html

<html>
  <head>
  </head>
  <body>
    {{ number_of_rooms }}
  </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2015-06-27
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多