Here I attach the relevant parts of the files "views.py", "index.html" and "forms.py" so you can check them out.
Thanks! El jueves, 25 de abril de 2024 a las 11:36:03 UTC-3, Ryan Nowakowski escribió: > Can you share some code snippets? > > > On April 24, 2024 6:28:18 PM CDT, Guido Luis Dalla Vecchia < > gld...@gmail.com> wrote: > >> Hello! I'm building my first website with Django and I've been stuck for >> the past two months with a problem I can't figure out. >> My website has a form with three fields that allow the user to input his >> first name, last name and email. >> In my "forms.py" file, I've declared "clean" methods for each field, that >> add the "error" class to them if they don't pass the validation. >> In my CSS file, I declared a rule that applies the "background-color: >> lightcoral" to all elements with the class ".error". >> The problem is that, when I fill in the "last name" field with invalid >> data, the "first name" field gets marked as invalid too. >> I've tried everything I could think of, and nothing has worked. It's >> driving me crazy!! Has anyone ever seen something like this? >> >> Pleas help!!! >> >> -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0e076efc-5da3-4020-8b80-d9a9b7b4b861n%40googlegroups.com.
from django.shortcuts import render from .forms import AppointmentForm def home(request): if request.method == "POST": form = AppointmentForm(request.POST) if form.is_valid(): form.save() return render(request, "home/index2.html", {"form": form}) else: form = AppointmentForm() return render(request, "home/index2.html", {"form": form})
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.name }}
{{ form.last_name }}
{{ form.email }}
from django import forms from .models import Appointment class AppointmentForm(forms.ModelForm): class Meta: model = Appointment fields = ['name', 'last_name', 'email'] widgets = { "name": forms.TextInput(attrs={"class": "form-control"}), "last_name": forms.TextInput(attrs={"class": "form-control"}), "email": forms.EmailInput(attrs={"class": "form-control"}), } def clean_name(self): name = self.cleaned_data['name'] if not name.isalpha(): self.fields['name'].widget.attrs['class'] += ' error' raise forms.ValidationError("Please, input only letters", code="carac_esp") return name def clean_last_name(self): last_name = self.cleaned_data["last_name"] if not last_name.isalpha(): self.fields['last_name'].widget.attrs['class'] += ' error' raise forms.ValidationError("Please, input only letters", code="carac_esp") return last_name def clean_email(self): email = self.cleaned_data["email"] allowed_domains = ['gmail.com', 'hotmail.com', 'yahoo.com'] if not any(email.endswith(dominio) for dominio in allowed_domains): self.fields['email'].widget.attrs['class'] += ' error' raise forms.ValidationError("Please, input a valid email address", code="invalid_email") return email
- Problem to style form fields with error Guido Luis Dalla Vecchia
- Re: Problem to style form fields with error Guido Luis Dalla Vecchia