Django视图和模板

时间:2016-02-17 13:19:22

标签: html django

我有包含

的product_view.html文件
Product {{product.title}}

我有查看哪些已加载有关模型的信息

template = loader.get_template(path)
context = {'title': product[0].title, 'description': product[0].description, 'image_url': product[0].image_url, 'quantity': product[0].quantity}
return HttpResponse(template.render(context, request))

如何将数据呈现给product_view.html,以便显示product.title?现在它只显示"产品"在页面上。我是django的新手,而且我甚至无法做到这一点:(

1 个答案:

答案 0 :(得分:2)

首先让一切变得简单明了!

template = 'YourTemplateFolder/product_view.html'
context = {"product":product[0]}
return render(request, template, context)

对于模板,要在其中打印任何内容,只需将变量名称放在两个大括号内

{{ product.title }} 
# product is the name you defined in context and by default it will take all of the variables attached to the original variable

并且请在将来为问题添加更多详细信息,以便人们可以帮助您而不是向您投票。