我正在使用django-allauth,在网站上进行注册后,该用户被发送到个人资料页面,但是我希望用户在注册后看到一条消息“您的帐户已创建”,然后转到登录页面。
有什么办法吗?
答案 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})