Django错误:未返回HttpResponse对象。它返回None

时间:2019-12-27 23:43:57

标签: django django-forms

我的格式如下:

class PrestataireProfilForm(forms.ModelForm):
    class Meta:
        model = Prestataire
        fields = ["user", "name", "address", "phone", "fax", "city"]

    def __init__(self, *args, **kwargs):
        super(PrestataireProfilForm, self).__init__(*args, **kwargs)
        self.fields['city'].widget.attrs.update({'class' : 'custom-select'})
        self.fields['city'].label = ""

这是我的观点:

@login_required
def prestataire_profil(request):
    prestataire = Prestataire.objects.filter(user=request.user).first()
    is_prestataire = request.user.groups.filter(name='Prestataire').exists()
    form = PrestataireProfilForm(request.POST or None, instance=prestataire)
    if request.method == 'POST':
        context = {
            'profil': prestataire,
            'is_prestataire': is_prestataire,
            'form': form
        }
        if form.is_valid():
            prestataire = form.save(commit=False)
            prestataire.save()
            context = {
            'profil': prestataire,
            'is_prestataire': is_prestataire
            }
            # return render(request, 'dashboard/prestataires/profil.html', context)
            return redirect('prestataire_profil')
    else:
        context = {
            'profil': prestataire,
            'is_prestataire': is_prestataire,
            'form': form
        }
        return render(request, 'dashboard/prestataires/profil.html', context)

这是我的html表单:

<form method='POST'>
                {% csrf_token %}
                <div class="form-group row">
                  <label for="name" class="col-4 col-form-label">Nom d'utilisateur</label> 
                  <div class="col-8">
                    <input value="{{ profil.user.username }}" id="name" name="name" placeholder="Nom d'utilisateur" class="form-control here" required="required" type="text" disabled>
                  </div>
                </div>
                <div class="form-group row">
                  <label for="name" class="col-4 col-form-label">Nom*</label> 
                  <div class="col-8">
                    <input value="{{ profil.user.last_name }}" id="name" name="name" placeholder="Nom" class="form-control here" required="required" type="text">
                  </div>
                </div>
                <div class="form-group row">
                  <label for="name" class="col-4 col-form-label">Prénom*</label> 
                  <div class="col-8">
                    <input value="{{ profil.user.first_name }}" id="name" name="name" placeholder="Prénom" class="form-control here" required="required" type="text">
                  </div>
                </div>
                <div class="form-group row">
                  <label for="name" class="col-4 col-form-label">Raison sociale*</label> 
                  <div class="col-8">
                    <input value="{{ profil.name }}" id="name" name="name" placeholder="Raison sociale" class="form-control here" required="required" type="text">
                  </div>
                </div>
                <div class="form-group row">
                  <label for="address" class="col-4 col-form-label">Adresse</label> 
                  <div class="col-8">
                    <input value="{{ profil.address }}" id="address" name="address" placeholder="Adresse" class="form-control here" type="text">
                  </div>
                </div>
                <div class="form-group row mb-0">
                  <label for="address" class="col-4 col-form-label">Ville</label> 
                  <div class="col-8">
                    {{ form.city|as_crispy_field }}
                  </div>
                </div>
                <div class="form-group row">
                  <label for="phone" class="col-4 col-form-label">Téléphone</label> 
                  <div class="col-8">
                    <input id="phone" name="phone" placeholder="Téléphone" class="form-control here" type="text">
                  </div>
                </div>
                <div class="form-group row">
                  <label for="fax" class="col-4 col-form-label">Fax</label> 
                  <div class="col-8">
                    <input value="" id="fax" name="fax" placeholder="Fax" class="form-control here" type="text">
                  </div>
                </div>
                <div class="form-group row">
                  <div class="offset-4 col-8">
                    <button name="submit" type="submit" class="btn btn-primary">Update My Profile</button>
                  </div>
                </div>
              </form>

当我单击“提交”按钮时,出现以下错误:

异常类型:ValueError 异常值:视图dashboard.views.prestataire_profil没有返回HttpResponse对象。它返回None。

数据不会更改,并且调试中没有任何信息可以查看出了什么问题。您可以在视图中注意到,我评论了return渲染线。我将其更改为返回重定向,但存在相同的问题! 请帮忙!谢谢。

1 个答案:

答案 0 :(得分:0)

沙鲁特!

我认为您的表格无效(您检查过吗?)。您已处理案件if form.is_valid(),但如果没有,该怎么办?那你什么都不退!因此,如果在所有情况下都将重定向到prestataire_profil,则应执行以下操作:

@login_required
def prestataire_profil(request):
    [...]
    if request.method == 'POST':
        [...]
        if form.is_valid():
            [...] # do your stuff 
            # return redirect('prestataire_profil') <--- wrong indentation
        return redirect('prestataire_profil') # <--- there, it will return something anyway
    else:
        [...]
        return render(request, 'dashboard/prestataires/profil.html', context)

try... except... finally...语句会更好,因为这样做不会阻止您返回None而不是预期的HttpResponse object