CreateView 与 UpdateView
根据模型构建添加和编辑页面
CreateView 与 UpdateView 是 CoddyKit 上的免费 Django Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。
「CreateView 与 UpdateView」这节课中我会学到什么?
根据模型构建添加和编辑页面 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Django Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「CreateView 与 UpdateView」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Django Academy 课中编写并运行代码吗?
能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- ListView 与 DetailView
- CreateView 与 UpdateView
- DeleteView 与 success_url
- 重写 get_queryset 与模板