Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1K
Visualizações
Django: How to make form conditional?

I have a form with two fields. The user should be required to only select one of the two. Not both and not none. I tried solving this by overwriting the clean method as described in the Django Doc:

forms.py

class ConfigureWorkout(forms.Form):
    first_exercise = forms.ModelChoiceField(empty_label="select", label="Choose First Exercise", queryset=FirstExercise.objects.all(), required=False) 
    sec_exercise = forms.ModelChoiceField(empty_label="select", label="Choose Sec Exercise", queryset=SecExercise.objects.all(), required=False)

    def clean(self):
        first_exercise = self.cleaned_data.get("first_exercise")
        sec_exercise = self.cleaned_data.get("sec_exercise")

        if first_exercise and sec_exercise:
            raise forms.ValidationError("Enter either a First Exercise or a Secondary Exercise.")
        else:
            return self.cleaned_data

views.py

def configure(request):
    configure_workout = ConfigureWorkout()

    if request.method == "GET":
        return render(request, "userprofile/some.html", configure_workout)

    else:
        return render(request, "app/other.html")

template

<form action="{% url 'configure' %}" method="POST">
    {% csrf_token %}
    {{ configure_workout }}
    <input type="submit" name="configuration_completed">
</form>

However, if I test this by selecting both fields in the form, there won't be an error displayed/raised. I pass the form successfully and get sent to "other.html".

What am I missing? Thanks so much in advance for any help :)

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

It seems you're not passing actual data to your form. Maybe this will help:

def configure(request):
    configure_workout = ConfigureWorkout()

    if request.method == "GET":
        return render(request, "userprofile/some.html", configure_workout)

    else:
        configure_workout = ConfigureWorkout(request.POST)
        configure_workout.is_valid()
        configure_workout.clean()
        return render(request, "app/other.html")
over 4 years ago · Santiago Trujillo Relatório

0

Building on @trafalinos answer and looking at the documentation on Forms, I would suggest doing the following:

In forms.py:

class ConfigureWorkout(forms.Form):
    first_exercise = forms.ModelChoiceField(empty_label="select", label="Choose First Exercise", queryset=FirstExercise.objects.all(), required=False) 
    sec_exercise = forms.ModelChoiceField(empty_label="select", label="Choose Sec Exercise", queryset=SecExercise.objects.all(), required=False)

    def clean(self):
        cleaned_data = super().clean()  # compare documentation
        first_exercise = cleaned_data.get("first_exercise")
        sec_exercise = cleaned_data.get("sec_exercise")

        if first_exercise and sec_exercise:
            raise forms.ValidationError("Enter either a First Exercise or a Secondary Exercise.")
        else:
            return self.cleaned_data

And (credits to trafalino) in views.py:

def configure(request):
    if request.method == "GET":
        configure_workout = ConfigureWorkout()
        return render(request, "userprofile/some.html", configure_workout)

    else:
        configure_workout = ConfigureWorkout(request.POST)
        configure_workout.clean()
        return render(request, "app/other.html")
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda