使用django-allauth注册后重定向到自定义验证页面 [英] Redirect after signup using django-allauth to custom verification page

查看:161
本文介绍了使用django-allauth注册后重定向到自定义验证页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的注册页面,我想在创建用户后发送电子邮件验证。但是,我想在注册后将用户重定向到其他模板,该模板向他们显示一条消息,提示他们需要验证其电子邮件地址。

I have a custom signup page and I would like to send email verification after user creation. However, I would like to redirect users to a different template after signup which shows them a message that they need to verify their email address.

当前,我的观点是:

def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)  
            #user.is_active = True
            user.save()
            send_email_confirmation(request, user, True)
    else:
        form = SignUpForm()
    return render(request, 'main/signup.html', {'form': form})

如何将用户重定向到模板 verification_sent.html ?我还实现了一种方法,供用户在不正确的情况下更改电子邮件地址,但找不到如何将其集成到我的 verification_sent.html 模板中。

How can I redirect users to a the template verification_sent.html? I have also implemented a method for users to change the email address if incorrect but I cannot find how I can integrate that in my verification_sent.html template.

推荐答案

在此示例中,我们使用ajax将用户注册数据发送到后端。如果后端成功注册了新用户,我们将返回一个响应对象。检索响应对象后,js会将用户重定向到验证页面。

In this example we use ajax to send the user registration data to the backend. If the backend successfully registers a new user we return a response object. Upon retrieving the response object, js will redirect the user to the verification page.

script.js:

script.js:

function register() {

  // conduct ajax request:
  $.ajax({
    url : 'register',
    data : {
      csrfmiddlewaretoken : 'the_token',
      username : 'the_username',
      password : 'the_password',
    },
    success : register_success, // reference to function below:
  })

} $('#register').click(register); // execute function when register button is clicked

// executes upon retrieving a successful response from backend:
function register_success(response) {

  // unpack response:
  status = response.status;
  
  // redirect users with okay status:
  if (status=='okay') window.location('verification');

}

views.py:

def register(request):

   # unpack request:
   username = request.POST['username']
   password = request.POST['password']

   # register user, send email:
   ...

   # pack response:
   response = json.dumps({
       'status' : 'okay' # or other status for failed registration attempts
   })

   return HttpResponse(response)

这篇关于使用django-allauth注册后重定向到自定义验证页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆