0Pricing
Django Academy · Lesson

login_required and Protecting Views

Restrict pages to authenticated users.

login_required and Protecting Views is a free Django Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. ✅

Frequently asked questions

Is the “login_required and Protecting Views” lesson free?

Yes — the full text of “login_required and Protecting Views” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “login_required and Protecting Views”?

Restrict pages to authenticated users. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “login_required and Protecting Views” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The User Model and authenticate()
  2. login, logout, and Auth Views
  3. Registration with UserCreationForm
  4. login_required and Protecting Views
← Back to Django Academy