注册后将用户重定向到登录页面

时间:2020-05-16 14:20:13

标签: python django django-allauth

我正在使用django-allauth,在网站上进行注册后,该用户被发送到个人资料页面,但是我希望用户在注册后看到一条消息“您的帐户已创建”,然后转到登录页面。

有什么办法吗?

2 个答案:

答案 0 :(得分:2)

您可以先从get_login_redirect_url()中覆盖django-allauth方法。

然后使用 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

评论后答复更新:

要在创建帐户后注销。

from django.contrib.auth import logout

logout(request)  # --> put it before return above

答案 1 :(得分:0)

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})