context_data = {}
all_answers = Answer.objects.all()
for i in range(0, len(all_answers)) :
all_answers[i].text = modify_text(all_answers[i].text, request)
context_data ["all_answers"] = all_answers
print(context_data ["all_answers"][0].text) #Check that it was modified
return render(request, 'template.html', context_data)
在我的模板中,我喜欢:
{% for answer in all_answers.all %}
{{ answer.text}}
{% endfor %}
检查表明已进行了修改,但它是我的模板answer.text是数据库中未修改的数据。
我看到context_data[“all_answers”]的类型是一个queryset,我猜它是在模板呈现期间触发的,使得任何未保存的更改都无用,如何使我的更改显示在模板中?
context_data ["all_answers"] = list(all_answers)
加载查询集。检查有效,但模板中没有显示任何内容
(1)
在模板呈现过程中,当我使用
this function
将queryset加载到dict列表中。
3
我不想保存更改,因为它们是为每个请求定制的(基本上是为每个用户)。
PS:使用Python3.6.8和Django 2.2.3。