【发布时间】:2019-12-19 03:32:51
【问题描述】:
在我的views.py中,我这样做了:
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"] 的类型是一个查询集,我猜是在模板渲染期间触发的,使任何未保存的更改无用,如何使我的更改显示在模板中?
我试过了:
context_data ["all_answers"] = list(all_answers)
加载查询集。检查有效,但模板中没有任何显示 (1)
当我使用this function 将查询集加载到字典列表中时,在模板渲染期间出现错误。
我还看到了一个[没有答案的链接问题]。3
我不想保存更改,因为它们是针对每个请求定制的(基本上是针对每个用户)。
** TLDR:如何在不将更改保存到数据库的情况下查看我在模板中的修改?**
PS:使用 Python 3.6.8、Django 2.2.3。
【问题讨论】:
标签: python django python-3.x