Search code examples
djangodjango-viewsdjango-generic-views

success_url for Django LoginView?


High level question: Can I use the success_url attribute to change to where a view inheriting from Django's LoginView will redirect?

I have a login view that looks something like the below (ignoring import statements):

class MyLoginView(LoginView):
    template_name = 'myapp/mytemplate.html'
    success_url = reverse_lazy('myapp:my_success_view')

However, my login view still redirects to my LOGIN_REDIRECT_URL from my settings.py. Is this the wrong way to change my redirect url in the view?

Edit Since part of the issue might be the template, I'm including the submit button portion of my template. here is how it looks after taking into account Mehdi's answer (behavior did not change).

        <div class="d-flex justify-content-center mt-4" style="width: 100%">   
            <button type="submit" class="btn btn-primary" style="width: 100%">
                Log In
            </button>
        </div>
        <input name="next" type="hidden" value="{% url 'myapp:mytemplatename' %}"/>

Solution

  • In your template you should use a next input like this:

    <input name="next" type="hidden" value="{% url 'success_view' %}>

    You can custom its name with redirect_field_name attribute of your view. See class documentation here.