【问题标题】:The view xxx didn't return an HttpResponse object. It returned None instead视图 xxx 没有返回 HttpResponse 对象。它返回 None 而不是
【发布时间】:2020-10-16 13:06:22
【问题描述】:

我有一个我不明白的错误,因为我在视图中使用的逻辑没有改变并且适用于我的其他项目。当我阅读 Django 文档时,我的逻辑观点是一致的:https://docs.djangoproject.com/fr/2.2/topics/forms/#the-view

我们调用表单的 is_valid() 方法;如果不是 True,我们将返回带有表单的模板。这次表单不再为空(未绑定),因此 HTML 表单将填充之前提交的数据,可以根据需要对其进行编辑和更正。

在我的表单中,我有验证控制。 我下面的干净方法有 3 个验证控件和控件 #2

errors <ul class="errorlist"><li>__all__<ul class="errorlist nonfield"><li>Ce patient ne peut être randomisé dans ce site. Veuillez vérifier votre saisie.</li></ul></li></ul>

因此,在我对文档的理解中,它应该被重定向到带有错误消息显示的完整表单(就像在我的项目中通常那样),即使在我的视图中没有其他条件导致没有有效表单(is_valid() == 错误)。

怎么了?

views.py

def randomization_edit(request):
    if request.method == "POST":
        print("request", request.POST.dict())
        form = RandomizationEditForm(request, data=request.POST or None)
        if form.is_valid():
            # do stuff
            return redirect('randomization:confirmation', pk=randomisation.pk)

    else:
        if request.session.get('user_can_randomize'):
            form = RandomizationEditForm(request)
            return render(request, 'randomization/edit.html', {'form': form})
        else:
            return redirect("home")

forms.py

   def clean(self):
        cleaned_data = super(RandomizationEditForm, self).clean()
        # control #1
        if Randomisation.objects.filter(pat=self.data.get('pat').upper()).exists():
            bras = Randomisation.objects.get(pat=self.data.get('pat').upper()).bra_lib
            raise forms.ValidationError("Ce patient a déjà été randomisé dans le bras " + bras + ". Veuillez vérifier votre saisie.")
        # control #2
        if Pays.objects.get(pay_abr=self.data.get('pat').upper()[0:2]).pay_ide != int(self.data.get('pay_ide')):
            raise forms.ValidationError("Ce patient ne peut être randomisé dans ce site. Veuillez vérifier votre saisie.")
        # control #3
        if not patient_code_is_valid(self.data.get('pat').upper()):
            raise forms.ValidationError("Il y a incohérence entre le site et le pays dans le numéro patient. Veuillez vérifier votre saisie.")
        return cleaned_data

【问题讨论】:

    标签: django forms validation


    【解决方案1】:

    如果您发出 POST 请求,但表单无效,则不会返回任何结果。在这种情况下,通常会重新呈现包含错误表单的模板:

    def randomization_edit(request):
        if request.method == 'POST':
            form = RandomizationEditForm(request, data=request.POST)
            if form.is_valid():
                return redirect('randomization:confirmation', pk=randomisation.pk)
        else:
            if not request.session.get('user_can_randomize'):
                return redirect("home")
            else:
                form = RandomizationEditForm(request)
        return render(request, 'randomization/edit.html', {'form': form})

    return render(…) 将因此返回 HTTP 响应,以防它是带有 user_can_randomize 值的会话的 GET 请求,或者如果您发出 POST 请求,并且来自无效。

    【讨论】:

      猜你喜欢
      • 2017-12-28
      • 1970-01-01
      • 2021-10-06
      • 2017-01-25
      • 2020-12-03
      • 2018-09-28
      • 2017-08-01
      • 2019-06-18
      相关资源
      最近更新 更多