我试图将我的views.py中的数据传输到html页面。 如果views.py代码是这个
def VerifiedBySuperuser(request):
if request.method == 'POST':
vbs = MaanyaIT_EXAM48_ManageQuestionBank()
vbs.QuestionID = MaanyaIT_EXAM48_ManageQuestionBank.objects.get(QuestionID=request.POST.get(QuestionID, None))
vbs.QuestionInEnglishLang = request.POST.get('QuestionInEnglishLang', None)
vbs.save()
else:
return render(request, 'exam48app/verifiedbysuperuser.html')
那么html页面的代码是什么来查看我的所有数据到tamplates ..
这是我的html页面
<form class="from-horizontal" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="post-entry">
{{ MaanyaIT_EXAM48_ManageQuestionBank.QuestionInEnglishLang }}
</div>
</form>
现在该怎么办?
答案 0 :(得分:1)
从您的评论中,您需要知道如何从视图写入/呈现数据到html模板
我将为您演示一个简单的例子,
假设您有如下视图,
def VerifiedBySuperuser(request):
if request.method == 'GET':
context = {
"T_Name": "My Name",
"T_Age": 50,
"T_Phone": 1478523699
}
return render(request, 'verifiedbysuperuser.html', context=context)
和HTML模板如下,
<!DOCTYPE>
<html>
<body>
Name : {{ T_Name }}<br>
Age : {{ T_Age }}<br>
Phone : {{ T_Phone }}<br>
</body>
</html>
当您访问视图时,您将收到这样的响应,
在您的情况下,您可以将尽可能多的属性传递给模板dict
(在我的示例中显示)和模板/ html keys of context
(即T_Name
,T_Name
etct)变得多变。所以你可以直接在双括号内的HTML中使用它们({{ variable_name }}
)
据我所知,这是template rendering/ html rendering
的一般程序
的 UPDATE-1 强>
def VerifiedBySuperuser(request):
if request.method == 'POST':
obj = MyModel.objects.get(id=some_id)
other_data = [1,2,3,4,] # some specific data
context = {
"post_data": request.data,
"object_instance": obj,
"some_other_data": other_data
}
return render(request, 'verifiedbysuperuser.html', context=context)