我希望用户提交注册表单,然后重定向到另一个页面以验证他在手机号码字段中输入的号码。如果验证成功,则应创建我的用户,否则会出错。
目前我的注册视图如下所示:
def register_view(request):
template = "accounts/register.html"
print(request.user.is_authenticated())
next = request.GET.get('next')
print next
title = "Register"
form = UserRegisterForm(request.POST or None,)
if form.is_valid():
user = form.save(commit=False)
# number = user.mobile_number
# to_check = phonenumbers.parse(number,"TR")
# if phonenumbers.is_possible_number(to_check) and phonenumbers.is_valid_number(to_check):
# formatted_number = phonenumbers.format_number(to_check, phonenumbers.PhoneNumberFormat.E164)
# params = {
# 'api_key': ' api-key',
# 'api_secret': 'api-secret',
# 'number': formatted_number,
# 'brand': 'MobileVerification',
# }
# url = 'https://api.nexmo.com/verify/json?' + urllib.urlencode(params)
# response = urllib.urlopen(url)
# resp_dict = json.loads(response.read())
# request.session["user_request_id"] = resp_dict['request_id']
# messages.success(request,"A 4 digit pin has been successfully sent to your number.")
# return HttpResponseRedirect(reverse('pin_verify'))
# else:
# messages.error(request,"Enter a valid mobile number.")
# params2 = {
# }
# url = 'https://api.nexmo.com/verify/search/json?' + urllib.urlencode(params)
password = form.cleaned_data.get('password')
user.set_password(password)
user.save()
new_user = authenticate(username=user.mobile_number, password=password)
login(request, new_user)
if next:
return redirect(next)
return redirect("/")
context = {
"form": form,
"title": title
}
return render(request, template, context)
视图中的注释代码是我尝试获取数字然后重定向但但这不是应该如何。
我的注册模板如下所示:
{% extends "base.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block content %}
<div class='row'>
<div class='col-sm-6 col-sm-offset-3'>
<h1>Register for free!</h1>
<form method="post" action=".">
{% csrf_token %}
{{ form|crispy }}
<input class='btn btn-block btn-primary' type="submit" value="{% trans 'Join' %}" />
</form>
</div>
</div>
<hr/>
<div class='row'>
<div class='col-sm-6 col-sm-offset-3 text-align-center'>
<p>Need to <a href="{% url 'login' %}">Login</a>?</p>
</div>
</div>
{% endblock %}