0Pricing
Django Academy · บทเรียน

login_required และการปกป้องวิว

จำกัดหน้าให้เข้าถึงได้เฉพาะผู้ใช้ที่ยืนยันตัวตนแล้ว

login_required และการปกป้องวิว เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Some Pages Are Private

A dashboard or profile page should only open for signed-in users. Django makes protecting those views simple and consistent. 🚧

Meet login_required

The login_required decorator guards a view: anonymous visitors are bounced to the login page automatically.

from django.contrib.auth.decorators import login_required

Decorate a View

Place @login_required right above your view function. That single line is all it takes to lock the page down.

@login_required
def dashboard(request):
    return render(request, "dashboard.html")

Where Visitors Get Sent

When access is denied, Django redirects to the URL in LOGIN_URL. Set it in settings so the bounce lands on your login page.

LOGIN_URL = "/accounts/login/"

The next Parameter

The redirect adds a next query value with the original URL, so after login the user returns to the page they wanted. 🎯

Protecting Class-Based Views

For class-based views, use the LoginRequiredMixin instead of the decorator. List it first among the base classes.

from django.contrib.auth.mixins import LoginRequiredMixin

Mixin Placement Matters

The LoginRequiredMixin must come before the generic view in the class definition, or the protection will not apply.

class Dashboard(LoginRequiredMixin, TemplateView):
    template_name = "dashboard.html"

Check Inside a View

Sometimes you want manual control. Read request.user.is_authenticated and branch yourself instead of using the decorator.

if not request.user.is_authenticated:
    return redirect("login")

Permissions Go Further

To require a specific right, not just any login, use permission_required. It checks the user holds the named permission.

from django.contrib.auth.decorators import permission_required

Hide Links in Templates

In templates, wrap private links with user.is_authenticated so guests never even see buttons they cannot use.

Defense in Depth

Hiding a link is not real security. Always guard the view itself too, since users can type any URL directly.

Quick Check

Which setting decides where login_required sends anonymous users?

Recap: Protecting Views

You locked pages with login_required and LoginRequiredMixin, set LOGIN_URL, and learned to guard views, not just hide links. ✅

คำถามที่พบบ่อย

บทเรียน “login_required และการปกป้องวิว” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “login_required และการปกป้องวิว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “login_required และการปกป้องวิว”

จำกัดหน้าให้เข้าถึงได้เฉพาะผู้ใช้ที่ยืนยันตัวตนแล้ว คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “login_required และการปกป้องวิว” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. โมเดลผู้ใช้และ authenticate()
  2. login, logout และวิวการยืนยันตัวตน
  3. การลงทะเบียนด้วย UserCreationForm
  4. login_required และการปกป้องวิว
← กลับไปที่ Django Academy