CreateView y UpdateView
Cree páginas de alta y edición a partir de un modelo
CreateView y UpdateView es una lección gratuita de Django Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Django Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Django Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎉
Preguntas frecuentes
¿La lección «CreateView y UpdateView» es gratis?
Sí — el texto completo de «CreateView y UpdateView» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Django Academy, actualiza a CoddyKit PRO. El curso de Django Academy incluye 4 lecciones en total.
¿Qué aprenderé en «CreateView y UpdateView»?
Cree páginas de alta y edición a partir de un modelo Practicas Django Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Django Academy?
No se requiere experiencia previa. Django Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.
¿Cuánto tiempo toma la lección «CreateView y UpdateView»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Django Academy?
Sí. Cada lección de Django Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- ListView y DetailView
- CreateView y UpdateView
- DeleteView y success_url
- Sobrescritura de get_queryset y Templates