การลงทะเบียนด้วย UserCreationForm
เปิดให้ผู้ใช้สมัครสมาชิกได้อย่างปลอดภัย
การลงทะเบียนด้วย UserCreationForm เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Let People Sign Up
Login assumes accounts already exist. To let visitors create their own, you need a registration view backed by a sign-up form. ✍️
Meet UserCreationForm
Django gives you UserCreationForm, a ready form with username and two password fields that creates a User on save.
from django.contrib.auth.forms import UserCreationFormBuilt-In Password Checks
The form confirms the two passwords match and runs Django's strength validators, so weak or mismatched passwords are rejected automatically.
Show the Form on GET
On a GET request, create an empty form and render it. This is the blank sign-up page the visitor first sees.
form = UserCreationForm()
return render(request, "signup.html", {"form": form})Bind Data on POST
On a POST, build the form with request.POST so it holds the submitted values, ready to validate.
form = UserCreationForm(request.POST)Validate Then Save
Call is_valid(); if it passes, form.save() inserts the new user with a properly hashed password. One line creates the account.
if form.is_valid():
user = form.save()Log Them In Right Away
A smooth flow signs the user in immediately after sign-up. Call login() with the saved user so they skip a second step.
user = form.save()
login(request, user)Render the Errors
When validation fails, just render the same form again. It carries error messages that tell the user exactly what to fix.
The Sign-Up Template
Your template wraps the form in a POST and adds csrf_token. Output the fields and a submit button to finish the page.
Extending the Form
Need an email field? Subclass UserCreationForm and add it. The built-in form keeps things minimal until you ask for more.
Redirect After Success
Finish by sending the new user somewhere useful with a redirect, like their dashboard, instead of re-rendering the form.
return redirect("dashboard")Quick Check
What does UserCreationForm verify about the password automatically?
Recap: Registration
You used UserCreationForm to validate sign-ups, saved the new user with a hashed password, and logged them in right away. ✅
คำถามที่พบบ่อย
บทเรียน “การลงทะเบียนด้วย UserCreationForm” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การลงทะเบียนด้วย UserCreationForm” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การลงทะเบียนด้วย UserCreationForm”
เปิดให้ผู้ใช้สมัครสมาชิกได้อย่างปลอดภัย คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การลงทะเบียนด้วย UserCreationForm” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โมเดลผู้ใช้และ authenticate()
- login, logout และวิวการยืนยันตัวตน
- การลงทะเบียนด้วย UserCreationForm
- login_required และการปกป้องวิว