简体   繁体   English

Django视图和模板

[英]Django views and templates

I have product_view.html file which contains 我有product_view.html文件,其中包含

Product {{product.title}}

and I have view which have loaded information about model 并且我有加载了有关模型信息的视图

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))

How I can render data to product_view.html, so it shows product.title? 如何将数据呈现到product_view.html,以便显示product.title? Now it only shows "Product" on the page. 现在,它仅在页面上显示“产品”。 I am very new to django and it's very flustrating that I can't even do that :( 我是django的新手,而且我什至不能做到这一点非常沮丧:(

first make everything simple and clear! 首先让一切简单明了!

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

for the template, to print anything inside of it just put the variable name inside of two curly brackets 对于模板,要在其中打印任何内容,只需将变量名放在两个大括号内

{{ 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

and please in the future add more details to the question so people can help you instead of down-voting you. 并请日后在问题中添加更多详细信息,以便其他人可以为您提供帮助,而不必拒绝您的投票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM