3

I am using django-allauth , after signing up on site the user is sent to profile page but I want the user to go to the login page after signing up with a message "your account has been created"

Is there any way to do this?

Akhil Garg
  • 89
  • 9

2 Answers2

2

You can first override get_login_redirect_url() method from django-allauth.

And then for sending message use django messages framework.

settings.py:

ACCOUNT_ADAPTER = 'yourapp.adapter.AccountAdapter' # ---> change it to your path

yourapp/adapter.py:

from allauth.account.adapter import DefaultAccountAdapter
from django.contrib import messages

class AccountAdapter(DefaultAccountAdapter):
    def get_login_redirect_url(self, request):
        messages.success(request, 'Your account created.')
        return 'url/to/your/page' # --> change it to your page url

Answer update after comment:

To logout after account creation.

from django.contrib.auth import logout

logout(request)  # --> put it before return above
Astik Anand
  • 12,757
  • 9
  • 41
  • 51
  • After trying this I am getting infinite redirect loop on console and Page isn't redirecting properly on my browser. I am returning '/accounts/login' – Akhil Garg May 16 '20 at 14:52
  • @AkhilGarg, that will definitely happen. You are going to login page after login and that will again do login. So it will be infinite loop. Redirect to some different page like `/home` or something like that. – Astik Anand May 16 '20 at 15:25
  • Try that and do let me know if this worked for you. – Astik Anand May 16 '20 at 15:25
  • redirecting to `/home` is working. Is it possible that after a user signs up to the site rather then just logging in the user gets redirected to login page and then they have to login in order to access the site – Akhil Garg May 16 '20 at 15:29
  • Yeah possible, but it is not ideal. First you do logout suing code before redirect, but we shouldn't do it. – Astik Anand May 16 '20 at 15:32
  • how to logout before using redirect? – Akhil Garg May 16 '20 at 15:47
  • @AkhilGarg, check the updated answer. And if this has resolves your, please do vote up and mark the answer as accepted. Thanks !!! – Astik Anand May 16 '20 at 16:51
0

in views.py

def register(request):
users=User.objects.all()
if request.method == 'POST':
    form = RegisterForm(request.POST)
    if form.is_valid():
        form.save()
        username = form.cleaned_data.get('username')
        messages.success(request, f'Account has been created successfully!, User can now login')
        return redirect('login') #login is the name of login view in urls.py
else:
    form = RegisterForm()
return render(request, 'users/home/register.html', {'form': form,'users':users})
Mugoya Dihfahsih
  • 425
  • 3
  • 11