[Fixed]-Using Django Login Required Mixin

26👍

We should inherit the LoginRequiredMixin first. because python will consider the method dispatch from the first inherited class(in this case).

from django.contrib.auth.mixins import LoginRequiredMixin

class ArtWorkCreate(LoginRequiredMixin, CreateView):
    login_url = '/index/'
    redirect_field_name = 'index'
    model = ArtWork

5👍

Just Define a Login url on the settings.py file
(also remove the login url on the class)

#settings.py

LOGIN_URL = 'login'

0👍

login_url is URL that users who don’t pass the test/authentication will be redirected to.

redirect_field_name attribute should be set to URL the user should be redirected to after a successful login.

Source:https://docs.djangoproject.com/en/3.0/topics/auth/default/#django.contrib.auth.mixins.AccessMixin.get_login_url

Leave a comment