0
0
Djangoframework~10 mins

login_required decorator in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the login_required decorator from Django.

Django
from django.contrib.auth.decorators import [1]
Drag options to blanks, or click blank then click option'
Arequire_login
Blogin_required
Clogin_decorator
Dauth_required
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'require_login' instead of 'login_required'.
Trying to import from the wrong module.
2fill in blank
medium

Complete the code to apply the login_required decorator to a Django view function.

Django
@[1]
def dashboard(request):
    return render(request, 'dashboard.html')
Drag options to blanks, or click blank then click option'
Arequire_login
Buser_login
Cauth_required
Dlogin_required
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@require_login' which is not a valid decorator.
Forgetting the '@' symbol before the decorator.
3fill in blank
hard

Fix the error in the code by completing the decorator usage correctly.

Django
from django.contrib.auth.decorators import login_required

@login_[1]
def profile(request):
    return render(request, 'profile.html')
Drag options to blanks, or click blank then click option'
Arequired
Brequire
Crequireds
Drequires
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'login_require' or 'login_requires' instead of 'login_required'.
Misspelling the decorator name.
4fill in blank
hard

Fill both blanks to complete the code that redirects unauthorized users to a custom login page.

Django
@login_required(login_url='[1]')
def settings(request):
    return render(request, '[2]')
Drag options to blanks, or click blank then click option'
A/custom-login/
Bsettings.html
Clogin.html
D/accounts/login/
Attempts:
3 left
💡 Hint
Common Mistakes
Using the default login URL instead of a custom one.
Using the wrong template name.
5fill in blank
hard

Fill all three blanks to create a class-based view that requires login using the login_required decorator.

Django
from django.utils.decorators import method_decorator
from django.views import View

@method_decorator(login_required, name='[1]')
class AccountView(View):
    def [2](self, request):
        return render(request, '[3]')
Drag options to blanks, or click blank then click option'
Adispatch
Bget
Caccount.html
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Applying the decorator to 'get' instead of 'dispatch'.
Using 'post' method without handling GET.
Using a wrong template name.