15

Suppose a user is on the page /some_url/ on a site with django-allauth. When clicking "Login" they get sent to a URL like:

/accounts/login/?next=/some_url/

If they are already a registered user, after logging in here, they get sent to the /some_url/, which is fine.

But if they're not registered, and they click "sign up", they get sent to:

/accounts/signup/?next=/some_url/

Suppose I want to send the user to some onboarding experience, at /onboarding/, straight after they sign up.

What's the simplest way to override allauth's default behaviour, and send the user to /onboarding/ even if a next=/some_url/ is specified?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
awidgery
  • 1,896
  • 1
  • 22
  • 36

1 Answers1

19

The simplest way would be to override the template account/signup.html with your own copy. If you examine that template you will see the following section:

{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}

If you remove the if/endif part and change the value inside, the signup page will redirect to a specific page, even if you pass a next argument in the URL:

<input type="hidden" name="{{ redirect_field_name }}" value="/onboarding/" />
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • Although doesn't fit my case well, it's a good answer. In my case, I'm afraid not all users going to login immediately after signup. Then their URL may still get a "next" parameter. I'm going to use a middleware to do redirection, and use a counter to ensure the redirection only happen once. Ya, complicated solution, but can guarantee "/onboarding/" is visited once – ZZY Mar 23 '15 at 15:34
  • Would it not be better @Selcuk to do something like `value="{% url 'onboarding_urlname' %}"` – unlockme Feb 07 '18 at 10:47
  • @unlockme That would be ideal when possible. – Selcuk Feb 26 '18 at 02:53
  • Why not just use name="next" as opposed to name="{{ redirect_field_name }}" If you want a redirect to happen every time for that form? – alpalalpal Mar 08 '18 at 22:21
  • 2
    @alpalalpal Because `{{ redirect_field_name }}` is _usually_ `next`, but it doesn't have to be. It might have been changed using the `login_required` decorator, for example: `@login_required(redirect_field_name='next_url')` – Selcuk Mar 09 '18 at 01:19
  • +1 for `redirect_field_name` and `redirect_field_value`. Did not realize the `next` variable isn't available in the default `allauth` login view, so the template I had migrated over from Django's default authentication was not working correctly. – Mihai Chelaru Jul 13 '21 at 19:33