Using Loginrequiredmixin with Login Formview

Asked

Viewed 22 times

0

Basically, I have a Formview where I do validations to log users to my platform, if the result is positive, it is redirected to the URL /dashboard/, as shown in the code below.

class EntrarView(FormView):
    template_name = 'entrar.html'
    form_class = FormEntrar
    success_url = '/dashboard/'

    def get_context_data(self, **kwargs):
        context = super(EntrarView, self).get_context_data(**kwargs)
        return context

    def form_valid(self, form):
        if str(self.request.method)=='POST':
            # validações
            login(self.request, usuario)
            return super().form_valid(form)

This redirect is working perfectly, however, I have several Views which can only be accessed if the user is logged in. I am using the Loginrequiredmixin. However, when logging in, instead of the user being redirected to the success_url of Templateview (as follows example below), it continues to be redirected to the success_url of Formview.

class Carreiras_UsuarioView(LoginRequiredMixin, TemplateView):
    template_name = 'carreiras_usuario.html'
    login_url = '/entrar/'
    redirect_field_name = 'redirect_to'
    success_url = '/carreiras/'

In my urls.py have:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('dashboard/', views.DashboardView.as_view(), name='dashboard'),
    path('entrar/', views.EntrarView.as_view(), name='entrar'),
    path('carreiras/', views.Carreiras_UsuarioView.as_view(), name='carreiras_usuario'),
]

I’ve tried to change the redirect_field_name for next and nextpage but I did not succeed, as well as set the success_url of Templateview for reverse_lazy('core:carreiras_usuario') (the name of my app is "core").

Remembering that the class Carreiras_usuarioview is just one of several other views that I want to assign that same behavior, that is, depending on View that "calls" the Entrarview, i want to redirect the user to their respective URL.

1 answer

0

To make the view be used only if the user is logged in, see below:

Configure in Settings

LOGIN_REDIRECT_URL = 'login_page/'
LOGOUT_REDIRECT_URL = '/'

Decorate the view

from django.contrib.auth.decorators import login_required

@login_required()
def minha_view(request):
     # código aqui...

For more details, see here

  • But in fact Loginrequiredmixin does this same function, only it is more geared to CBV... And also I cannot set LOGIN_REDIRECT_URL in Settings because it is a variable that will change depending on the View that "call" the Formview...

  • vc can pass the redirect page inside the login_required as described in the documentation login_required(redirect_field_name='next', login_url=None). Regardless, you’re using the redirect_field_name? If you are not taking it out and testing again.

  • I checked the @login_required documentation, but as I mentioned before, it is only used in Function Based Views, which does not apply to my case, since I am developing in the Class Based View standard. And even the Fields passed as parameter to this function are already present in my Views. Anyway, I appreciate the answer, it will be useful for people going through this situation with FBV. I ended up solving my problem just inheriting the method get_success_url Formview and using the self.request.META.get('HTTP_REFERER') to identify which of the Views called the login :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.