אישור הגעה אחד לכל כתובת אימייל
חלק מהיחידה טפסים במסלול ה-Django של Coddy. שיעור 18 מתוך 20.
הפרק הזה שלך: שלוש תכונות ללוח האירועים, שנבנות על מה שכבר למדת. כל משימה מציינת מה האתר צריך לעשות; הקוד הוא בידיים שלך.
כרגע אדה יכולה להירשם לערב הג'אז של יום שישי כמה פעמים שתרצה, וכל הרשמה תופסת מקום. כתובת אימייל אחת צריכה לתפוס מקום אחד לכל אירוע.
כל החלקים בידיך: מתודת clean_<field>, האירוע שהטופס שומר ב-self.event, וההרשמות אליו ב-self.event.rsvps. שאילתה נוספת תעזור: email__iexact משווה טקסט בלי להתחשב באותיות רישיות או קטנות, כי Ada@Example.com ו-ada@example.com מגיעות לאותה תיבת דואר.
אתגר
בינונידחו RSVP שני מאותה כתובת אימייל לאותו אירוע, והציגו את This email is already on the guest list. לצד שדה האימייל.
- לא משנה אם האותיות גדולות או קטנות:
ADA@Example.comהיאada@example.com. - עדיין אפשר לשלוח RSVP לאירוע אחר עם אותה כתובת אימייל.
שלחו RSVP לערב Friday Jazz Night בתור ADA@example.com: אתם רואים את This email is already on the guest list.
נסו בעצמכם
import datetime
from django import forms
from .models import RSVP, Event
class EventForm(forms.ModelForm):
class Meta:
model = Event
fields = ["title", "date", "end_date", "place", "description", "capacity"]
def clean_date(self):
date = self.cleaned_data["date"]
if date < datetime.date.today():
raise forms.ValidationError("The date cannot be in the past.")
return date
def clean(self):
cleaned_data = super().clean()
date = cleaned_data.get("date")
end_date = cleaned_data.get("end_date")
if date and end_date and end_date < date:
raise forms.ValidationError("The event cannot end before it starts.")
return cleaned_data
class RSVPForm(forms.ModelForm):
class Meta:
model = RSVP
fields = ["name", "email"]
def __init__(self, *args, event=None, **kwargs):
super().__init__(*args, **kwargs)
self.event = event
def clean(self):
cleaned_data = super().clean()
if self.event.rsvps.count() >= self.event.capacity:
raise forms.ValidationError("Sorry, this event is full.")
return cleaned_data
כל השיעורים ביחידה טפסים
תרגלו בעצמכם: Django אונליין