CreateView و UpdateView
إنشاء صفحتي الإضافة والتحرير من نموذج
CreateView و UpdateView درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Pages That Write Data
Showing data is half the job. To add and edit records you reach for CreateView and UpdateView.
Meet CreateView
CreateView builds an add page: it renders a form, validates input, and saves a new record for you.
A Form from Fields
Tell CreateView the fields you want, and it generates a matching form straight from your model.
from django.views.generic import CreateView
class PostCreate(CreateView):
model = Post
fields = ["title", "body"]The Form Template Name
CreateView and UpdateView both look for app/model_form.html, like post_form.html, by default.
Rendering the Form
Inside the template the generated form arrives as form, so you render its fields and a submit button.
<form method="post">{% csrf_token %}
{{ form.as_p }}
<button>Save</button>
</form>Meet UpdateView
UpdateView works just like CreateView, but it loads an existing record first so the form starts pre-filled.
Editing an Existing Record
UpdateView reads a pk from the URL, fetches that object, and saves your changes back to it.
from django.views.generic import UpdateView
class PostUpdate(UpdateView):
model = Post
fields = ["title", "body"]Where to Go After Saving
After a successful save, the view redirects. Set success_url to control exactly where users land next.
from django.urls import reverse_lazy
class PostCreate(CreateView):
success_url = reverse_lazy("post_list")Or Let the Model Decide
Skip success_url by giving your model a get_absolute_url; the view will redirect there automatically.
def get_absolute_url(self):
return reverse("post_detail", args=[self.pk])Wiring the Edit URLs
Create needs no pk, but update does, so its URL captures one with as_view() like the others.
path("new/", PostCreate.as_view()),
path("<int:pk>/edit/", PostUpdate.as_view()),Shared Form Power
Because both reuse the same form and template, add and edit pages stay consistent with almost no extra code. 💪
Quick Check
What is the main difference between CreateView and UpdateView?
Recap: Create and Update
You used CreateView to add records and UpdateView to edit them, shared one form template, and steered redirects with success_url. 🎉
الأسئلة الشائعة
هل درس «CreateView و UpdateView» مجاني؟
نعم — نص درس «CreateView و UpdateView» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «CreateView و UpdateView»؟
إنشاء صفحتي الإضافة والتحرير من نموذج تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «CreateView و UpdateView»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- ListView و DetailView
- CreateView و UpdateView
- DeleteView و success_url
- تجاوز get_queryset والقوالب