本文整理匯總了Python中accounts.forms.RegistrationForm.add_error方法的典型用法代碼示例。如果您正苦於以下問題:Python RegistrationForm.add_error方法的具體用法?Python RegistrationForm.add_error怎麽用?Python RegistrationForm.add_error使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類accounts.forms.RegistrationForm
的用法示例。
在下文中一共展示了RegistrationForm.add_error方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: register
# 需要導入模塊: from accounts.forms import RegistrationForm [as 別名]
# 或者: from accounts.forms.RegistrationForm import add_error [as 別名]
def register(request):
next_url = request.GET.get('next', None)
if request.method == 'GET':
form = RegistrationForm()
return render(request, 'registration/registration_page.html', {'registration_form': form})
if request.method == 'POST':
# create a form instance and populate it with data from the request
form = RegistrationForm(request.POST, )
if form.is_valid():
client = request.session['client']
new_user = form.save(commit=False)
try: # create a user on the API
data = client.create_user(new_user.email, request.POST['password1'],
new_user.first_name, new_user.last_name,
new_user.phone_number)
new_user.booker_id = data.get('CustomerID', None)
if not new_user.booker_id:
raise ValidationError(data['ErrorMessage'],
code=data['ErrorCode'])
new_user.save()
except ValidationError as error:
form.add_error(None, error)
return render(request, 'registration/registration_page.html',
{'registration_form': form})
# login to the api
client.login(new_user.email, request.POST['password1'])
# authenticate and login the user locally
new_user = authenticate(email=request.POST['email'].lower(),
password=request.POST['password1'])
auth_login(request, new_user)
try:
request.cart.replace(request.session.get('CART-ID'), new_user)
except CartDoesNotExist:
pass
# store the user password for the length of the session
client.user = new_user
messages.info(request, 'Thanks for registering. You are now logged in.')
# redirect user the profile page
if next_url:
return HttpResponseRedirect(next_url)
return HttpResponseRedirect(reverse('welcome'))
return render(request, 'registration/registration_page.html',
{'registration_form': form, 'next': next_url})