我在使用django formset时遇到了麻烦。发出POST请求会导致带有表单的ValidationErrors。我看了SO上的其他帖子,但没有一个答案对我有用。有什么想法吗?
class CompileTime(models.Model):
changelist = models.ForeignKey(Changelist)
corpus = models.CharField(max_length = 100)
frequency = models.IntegerField()
asmTollvmIR = models.OneToOneField(TimeStatObj, related_name = "asmTollvmIR")
optimizationPasses = models.OneToOneField(OptimizationPassesTimeStat)
CompileTimeForm = modelformset_factory(CompileTime)
我的views.py看起来像这样。
@csrf_exempt
def add_form_data(request, formsetclass, custom_logic=None):
response_obj = create_response(False)
print formsetclass
if (request.method == "POST"):
try:
formset = formsetclass(data=request.POST)
except ValidationError:
formset = None
print formset
if (formset and formset.is_valid()):
if (custom_logic != None):
response_obj = custom_logic(formset, response_obj)
response_obj.update({"status": 0})
return json_response_from(response_obj)
else:
response_obj.update({"status":-1, "response_message": "Form is invalid"})
return json_response_from(response_obj)
elif (request.method == "GET"):
formset = formsetclass()
return render_to_response("dashboard/show_form.html",
{"formset": formset},
context_instance=RequestContext(request))
@csrf_exempt
def add_compile_time_stats(request):
return add_form_data(request, CompileTimeForm, save_form)
模板看起来像这样。
<form action="/dashboardMain/add_changelist" method="post">
{{ formset.management_form }}
<table>
{% for form in formset.forms %}
{{ form }}
{% endfor %}
</table>
<input type="submit" value="Send" /> <input type="reset">
</form>
答案 0 :(得分:0)
我意识到的问题是我没有从add_compile_time_stats函数返回。添加return语句会导致问题消失。 :) 之前应该看一下。