אישור הגעה אחד לכל כתובת אימייל
חלק מהיחידה טפסים במסלול ה-Django של Coddy. שיעור 18 מתוך 20.
הפרק הזה שלך: שלוש תכונות ללוח האירועים, שנבנות בעזרת מה שכבר למדת. בכל משימה מצוין מה האתר צריך לעשות; הקוד נתון לבחירתך.
כרגע Ada יכולה להירשם לערב הג׳אז של יום שישי כמה פעמים שהיא רוצה, וכל הרשמה תופסת מקום. כתובת אימייל אחת צריכה לתפוס מקום אחד בכל אירוע.
יש לך את כל מה שצריך: מתודת clean_<field>, האירוע שהטופס שומר ב־self.event, וההרשמות שלו ב־self.event.rsvps. חיפוש נוסף יכול לעזור: email__iexact משווה טקסט בלי להתחשב באותיות גדולות וקטנות, כי Ada@Example.com ו־ada@example.com מגיעות לאותה תיבת דואר נכנס.
אתגר
בינוניסרבו לאישור הגעה נוסף מאותה כתובת אימייל לאותו אירוע, והציגו את This email is already on the guest list. מתחת לשדה האימייל.
- אין חשיבות לאותיות גדולות או קטנות:
ADA@Example.comזהה ל־ada@example.com. - אותה כתובת אימייל עדיין יכולה לאשר הגעה לאירוע אחר.
לחצו על Run ואשרו הגעה פעמיים לערב הג׳אז של יום שישי באמצעות כתובת אימייל אחת.
נסו בעצמכם
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 אונליין