0
0
Djangoframework~20 mins

Why Django built-in auth matters - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Auth Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use Django's built-in authentication system?

Which of the following is the main advantage of using Django's built-in authentication system instead of creating your own from scratch?

AIt provides a secure, tested, and ready-to-use user management system with minimal setup.
BIt allows you to avoid writing any code for user login and registration forever.
CIt automatically creates a user interface for your entire website without customization.
DIt disables all security features so you can customize everything freely.
Attempts:
2 left
💡 Hint

Think about what saves time and reduces errors when handling user accounts.

component_behavior
intermediate
2:00remaining
What happens when you use Django's login view?

When you use Django's built-in LoginView in your app, what is the expected behavior after a successful login?

AThe user is logged out immediately after logging in.
BThe user is authenticated and redirected to the URL specified by <code>next</code> or a default page.
CThe user is redirected to the admin panel regardless of permissions.
DThe user is logged in but stays on the login page without any message.
Attempts:
2 left
💡 Hint

Think about what a good login flow should do after success.

state_output
advanced
2:00remaining
What is the output of this Django authentication code snippet?

Consider this Django view code snippet:

from django.contrib.auth import authenticate, login
from django.http import HttpResponse

def my_view(request):
    user = authenticate(request, username='alice', password='secret')
    if user is not None:
        login(request, user)
        return HttpResponse('Logged in')
    else:
        return HttpResponse('Failed login')

What will be the HTTP response if the username or password is incorrect?

A"Failed login"
BRaises a TypeError
C"Logged in"
DRedirects to the login page automatically
Attempts:
2 left
💡 Hint

What does authenticate return if credentials are wrong?

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this Django URL pattern for authentication

Which option contains a syntax error in defining a URL pattern for Django's built-in login view?

from django.urls import path
from django.contrib.auth.views import LoginView

urlpatterns = [
    path('login/', LoginView.as_view(), name='login'),
]
Apath('login/', LoginView.as_view(), name='login')
Bpath('login/', LoginView.as_view(), 'login')
Cpath('login/', LoginView(), name='login')
Dpath('login/', LoginView.as_view, name='login')
Attempts:
2 left
💡 Hint

Remember how to call class-based views in URL patterns.

🔧 Debug
expert
3:00remaining
Why does this Django custom user model cause an error?

Given this custom user model code snippet:

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    email = models.EmailField(unique=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

When running migrations, Django raises an error about missing fields. What is the cause?

AThe <code>USERNAME_FIELD</code> must be 'username' when extending AbstractUser.
BThe <code>email</code> field cannot be unique in a custom user model.
CThe <code>username</code> field is required but not set as unique or primary identifier.
DThe <code>REQUIRED_FIELDS</code> list must be empty when changing <code>USERNAME_FIELD</code>.
Attempts:
2 left
💡 Hint

Think about what Django expects when you change the USERNAME_FIELD.