如何在Django中创建更新表单表

时间:2020-05-24 10:35:40

标签: django

我希望能够以表格格式更新记录,以便可以快速进行更新。我即将解决这个问题,但是form.valid()仍在返回False

我的模特:

class Actions(models.Model):
    meeting = models.ForeignKey(Meeting, on_delete=models.CASCADE)
    dateAdded = models.DateTimeField(default = timezone.now, editable = False)
    dateComplete = models.DateTimeField(null=True, blank=True)
    action = models.TextField(max_length=1000,)
    responsibility = models.ForeignKey(staff, on_delete=models.CASCADE, blank=True,null = True,)
    complete = models.BooleanField(default = False)

我的观点:

def actionItemsView(request):
    ActionFormSet = modelformset_factory(Actions, fields=('action', 'responsibility','complete','meeting','dateComplete'),max_num=1)
    if request.method == "POST":
        action_formset = ActionFormSet(request.POST, request.FILES,queryset=Actions.objects.filter())
        for action_form in action_formset:
            print(action_form.errors)
            if action_form.is_valid():
                action = action_form.save()
                return HttpResponseRedirect('/saved!/')
    else:
        formset = ActionFormSet(queryset=Actions.objects.filter(complete = False))
        return render(request, 'action_items.html', {'formset': formset})

我的模板:

<table class="table table-hover table-sm">
  <tr>
    <th>decision</th>
    <th>responsibility</th>
    <th>complete?</th>
    <th>meeting</th>
    <th>date complete</th>
    <th>submit</th>
  </tr>

{%for form in formset%}
<form method="post" enctype= multipart/form-data>
  <tr>
    {{ formset.management_form }}
    {{ form.management_form }}
    {% csrf_token %}
    <td>{{ form.action }}</td>
    <td>{{ form.responsibility }}</td>
    <td>{{ form.complete }}</td>
    <td>{{ form.meeting }}</td>
    <td>{{ form.dateComplete }}</td>
    <td><button type="submit">Save</button></td>
  </tr>
</form>
{% endfor %}
</table>

运行此命令时,模板将完全按照我的期望呈现,但是当我对某项进行任何更改并单击“提交”时,它将抛出The view meetings.views.actionItemsView didn't return an HttpResponse object. It returned None instead. 因为form.valid()为False

form.errors返回:

<ul class="errorlist"><li>id<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
<ul class="errorlist"><li>action<ul class="errorlist"><li>This field is required.</li></ul></li><li>meeting<ul class="errorlist"><li>This field is required.</li></ul></li><li>id<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
<ul class="errorlist"><li>action<ul class="errorlist"><li>This field is required.</li></ul></li><li>meeting<ul class="errorlist"><li>This field is required.</li></ul></li><li>id<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

但是我可以在模板中看到每个记录在下拉菜单中分配了一个会议...

1 个答案:

答案 0 :(得分:1)

视图Meetings.views.actionItemsView没有返回HttpResponse对象。它返回None。

鉴于这是错误,这意味着您没有返回任何HTTPRESPONSE,这是正确的,因为如果它是POST请求,则没有任何return语句。

if request.method == "POST":
    action_formset = ActionFormSet(request.POST, request.FILES,queryset=Actions.objects.filter())
    for action_form in action_formset:
        print(action_form.errors)
        if action_form.is_valid():
            action = action_form.save()

views.py中的所有方法都需要返回HTTPRESPONSE,因此只需尝试在其中添加任何类型的HTTP响应,它就可以解决您的问题。

相关问题