Django ManagementForm数据丢失或被篡改 [英] Django ManagementForm data is missing or has been tampered with
问题描述
我不断收到错误:
[u'ManagementForm data is missing or has been tampered with']
我也无法弄清楚为什么。这是我的观点:
I can't figure out why either. Here is my view:
def CreateWorkout(request):
WorkoutInlineFormSet = inlineformset_factory(workout,exercise)
if request.method == "POST" :
formset = WorkoutInlineFormSet(request.POST)
if formset.is_valid():
formset.save();
else:
formset = WorkoutInlineFormSet()
return render_to_response('submit.html',{'formset': formset},context_instance=RequestContext(request))
这里是我的模板:
<body>
<form method="POST" action ="">
{{ formset.management_form }}
<table>
{% for form in formset.forms %}
{{ form }}
{% endfor %}
</table>
</form>
</body>
我读过你必须包含 formset.management_form
,我有。我认为这将是一个简单的修复,但我无法弄清楚这个问题。
I've read that you have to include the formset.management_form
, and I have. I thought that would be an easy fix, but I haven't been able to figure out the problem.
推荐答案
当你使用内联表单集,您需要提供对象所关联的实例。
When you use inline formset, you need to provide the instance that the objects relate to.
# First, fetch the instance from the db
workout = code_that_fetches_instance()
if request.method == "POST" :
formset = WorkoutInlineFormSet(request.POST, instance=workout)
...
else:
formset = WorkoutInlineFormSet(instance=workout)
文档使用
See the example in the docs on using an inline formset in a view for more information.
如果锻炼
和练习
是您的模型,您应该遵循Python约会并重命名他们锻炼
和练习
。小写锻炼
应该是您的表单中的所有练习都链接到的实例。
If workout
and exercise
are your models, you should follow the python convention and rename them Workout
and Exercise
. Lowercase workout
should be the instance that all the exercises in your formset are linked to.
这篇关于Django ManagementForm数据丢失或被篡改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!