Mam formularz i za każdym razem, gdy formularz jest aktualizowany wiadomość jest wyświetlana "Twój profil został zaktualizowany". Ten komunikat jest pokazany, nawet jeśli w formie nie wykonano żadnych zmian. Jak mogę zapobiec formularzu pokazać wiadomość tylko wtedy, gdy zmiany zostały wykonane?

Views.py

@login_required(login_url='login')
def profilePage(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, 'Your account {} has been updated'.format(request.user.username))
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'members/profile.html', context)

Forms.py

class ProfileUpdateForm(forms.ModelForm):
    phone_number = PhoneNumberField(label=False, widget=forms.TextInput(attrs={'placeholder': 'Phone Number'}))
    image = forms.ImageField(label=False)

    class Meta:
        model = Profile
        fields = ['phone_number', 'image', ]

Wiadomość używana w szablonie

{% if messages %}
  <div class="prof-sucmessages">
      {% for message in messages %}
          <div class="alert-profile alert-{{ message.tags }} alert-dismissible" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
              </button>
              {{ message }}
          </div>
      {% endfor %}
  </div>
{% endif %}

Dziękuje Ci za pomoc!

0
paniklas 22 listopad 2020, 18:58

1 odpowiedź

Najlepsza odpowiedź

Możesz użyć metody form.has_changed(), aby sprawdzić, czy coś się zmieniło, którego można użyć w ten sposób:

if not u_form.has_changed() and not p_form.has_changed():
    # Don't perform the save operations and don't show a message
    # Return an empty successful message instead
    return HttpResponse(status=204)

Moim zdaniem może to stać się:

def profilePage(request):
    if request.method == 'POST':
        u_form = UserUpdateForm(request.POST, instance=request.user)
        p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
        if not u_form.has_changed() and not p_form.has_changed():
            # Don't perform the save operations and don't show a message
            # Return an empty successful message instead
            return HttpResponse(status=204)
        if u_form.is_valid() and p_form.is_valid():
            u_form.save()
            p_form.save()
            messages.success(request, 'Your account {} has been updated'.format(request.user.username))
            return redirect('profile')
    else:
        u_form = UserUpdateForm(instance=request.user)
        p_form = ProfileUpdateForm(instance=request.user.profile)

    context = {
        'u_form': u_form,
        'p_form': p_form
    }
    return render(request, 'members/profile.html', context)

if u_form.has_changed() or p_form.has_changed():
    // Don't perform the save operations and don't show a message
    // Return an empty successful message instead
    return HttpResponse(status=204)
1
Glenn D.J. 22 listopad 2020, 16:30