clean Yöntemleriyle Özel Doğrulama
Alan ve form düzeyinde doğrulama kuralları ekleyin.
clean Yöntemleriyle Özel Doğrulama, CoddyKit'te ücretsiz bir Django Academy dersidir. Bu, 4 dersinin 3. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Django Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Django Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
Beyond Built-in Rules
Model fields give you basic checks, but real apps need custom rules. Django runs your own clean methods to enforce exactly the logic you want. 🧹
Field-Level clean
To validate one field, add a method named clean_fieldname. Django calls it automatically during validation for that single field.
def clean_title(self):
title = self.cleaned_data["title"]
return titleRead from cleaned_data
Inside a clean method, pull the value from cleaned_data. By this point Django has already converted it to the right Python type for you.
title = self.cleaned_data["title"]Raise to Reject
If a value breaks your rule, raise a ValidationError. Django catches it and shows the message next to that field in the form.
from django.core.exceptions import ValidationError
raise ValidationError("Title is too short.")A Real Field Check
Here we reject titles shorter than five characters. Note you must return the value at the end so it stays in cleaned_data.
def clean_title(self):
title = self.cleaned_data["title"]
if len(title) < 5:
raise ValidationError("Too short.")
return titleAlways Return the Value
The golden rule of clean_fieldname: return the cleaned value. Forget it and the field becomes None, even when input was valid.
Form-Level clean
To compare two fields, override the whole-form clean method. It runs after every field-level check has finished.
def clean(self):
cleaned = super().clean()
return cleanedCall super().clean() First
Begin your form-level clean by calling super().clean(). That gives you the full cleaned_data dict gathered from the field checks.
cleaned = super().clean()
start = cleaned.get("start")
end = cleaned.get("end")Compare Two Fields
Now you can enforce cross-field rules, like making sure end comes after start. Raise ValidationError when the relationship is wrong.
if start and end and end < start:
raise ValidationError("End must be after start.")Attach Errors to a Field
In form-level clean you can target one field with add_error. The message then appears right beside that input, not at the top.
self.add_error("end", "End must be after start.")Order of Validation
Django runs each clean_fieldname first, then the form-level clean last. Knowing this order helps you place each rule in the right spot.
Quick Check
Where should a rule that compares two fields live?
Recap: Custom Validation
Well done! Use clean_fieldname for single fields and form-level clean for cross-field rules. Raise ValidationError to reject, and always return cleaned values. 🎉
Sıkça Sorulan Sorular
“clean Yöntemleriyle Özel Doğrulama” dersi ücretsiz mi?
Evet — “clean Yöntemleriyle Özel Doğrulama” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Django Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Django Academy kursu toplamda 4 dersten oluşur.
“clean Yöntemleriyle Özel Doğrulama” dersinde ne öğreneceğim?
Alan ve form düzeyinde doğrulama kuralları ekleyin. Django Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Django Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Django Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 3. dersidir.
“clean Yöntemleriyle Özel Doğrulama” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Django Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Django Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- ModelForm ve Meta.fields Tanımlama
- form.save() ve commit=False
- clean Yöntemleriyle Özel Doğrulama
- Widget'lar, Etiketler ve Yardım Metni