0
votes

I have the following form:

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 = ""

And this is my view:

@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)

And this is my html form:

<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>

When I click on the submit button, it gives me the following error:

Exception Type: ValueError Exception Value: The view dashboard.views.prestataire_profil didn't return an HttpResponse object. It returned None instead.

The data does not change, and there is no information whatsoever on the debug to see what went wrong. As you can notice in the view, I commented the return render line. I changed it to the return redirect, but same issues! Please help! Thank you.

1

1 Answers

0
votes

Salut !

What I think is that your form is not valid (have you checked it was?). You treated the case if form.is_valid(), but what if it is not? Then you are not returning anything! So, if in all cases you are going to redirect to prestataire_profil, you should do:

@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)

A try... except... finally... statement would be a better thing to do, because the way you did won't prevent you to return None instead of an expected HttpResponse object.