Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LoginView not using success_url #17

Closed
bastiaan85 opened this issue Jan 16, 2019 · 4 comments
Closed

LoginView not using success_url #17

bastiaan85 opened this issue Jan 16, 2019 · 4 comments
Labels
Q&A Questions and answers

Comments

@bastiaan85
Copy link

bastiaan85 commented Jan 16, 2019

I'm not sure if this is a clone of #2 or #3 but I can't get the CustomLoginView success_url property to work to redirect after a login. Adding debug prints to the code using your example Django project, it seems the actual redirection is performed using the hard coded LOGIN_REDIRECT_URL = '/' in the settings.py. Setting the success_url to any value has no effect on this process.

Also the current version 1.3.1 of the jquery.bootstrap.modal.forms.js seems to lack the successURL competely while the previous versions still processed it. Does this mean the feature is currently broken or am I missing something?

@bastiaan85
Copy link
Author

Using this SO answer I was able to work around the issue by including a hidden form element with name="next" to provide the redirect url to the LoginView:

{% load widget_tweaks %}

<form method="post" action="">
  {% csrf_token %}
  <input type="hidden" name="next" value="{% url 'appname:index' %}">
  <div class="modal-header">
    <h3 class="modal-title">Log in</h3>
   [...]

@trco
Copy link
Owner

trco commented Jan 16, 2019

The redirection really happens based on the LOGIN_REDIRECT_URL = '/' in the settings.py and success_url doesn't have an effect. Good catch. And at the same time good hack with the hidden input field and name="next". Regarding the successURL from .js, it was deprecated during implementation of Django messages.

I think that hidden input field and name="next" could provide some extra functionality for the package. Maybe I give it a shoot in the future.

@trco trco added the Q&A Questions and answers label Jan 16, 2019
@bastiaan85
Copy link
Author

To give my background story here: the reason I ran into this issue is that I want a redirect specifically to the index of the app the user was logging in at, so not the root of the project, meaning a global setting won't help me.

On a more general note I would expect it to default to the current page the user clicked the Log in button, like how most websites behave. Although most of them use an intermediate page, like Github here or Amazon for example, they then redirect to the previous page, not the root index.

Checking the Django docs on LoginView it seems that the class property extra_context is targeted to forward data to the template:

extra_context: A dictionary of context data that will be added to the default context data passed to the template.

And indeed providing success_url via that dict in the LoginView

class CustomLoginView(LoginAjaxMixin, SuccessMessageMixin, LoginView):
    authentication_form = CustomAuthenticationForm
    template_name = 'myappname/login.html'
    success_message = 'Success: You were successfully logged in.'
    extra_context = dict(success_url=reverse_lazy('myappname:index'))

and using success_url manually as the hidden form element's value

<input type="hidden" name="next" value="{{ success_url }}">

Works fine, so at least in that way the redirection value can be statically defined in the view again.

@trco
Copy link
Owner

trco commented Jan 29, 2019

@bastiaan85 That's great way for custom success_url. I'll update the README.rst. Thanks for great contribution.

@trco trco closed this as completed Jan 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Q&A Questions and answers
Projects
None yet
Development

No branches or pull requests

2 participants