我想在注册时将键字段设为可选。 但我不知道为什么我不能在这里使用== None。表单总是响应它期望键字段的数据。 如果没有提供密钥,我如何跳过此检查?仅应在用户提供密钥的情况下进行检查。
def signup(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
key = Key.from_blob(request.POST['key'].rstrip("\r\n"))[0]
if form.is_valid():
if key == None:
form.save()
messages.add_message(request, messages.INFO, "Thanks for you Registration, you are now able to login.")
return redirect(reverse('login'))
if key.key_algorithm == KeyAlgorithm.RSAEncryptOrSign:
form.save()
messages.add_message(request, messages.INFO, "Thanks for you Registration, you are now able to login.")
return redirect(reverse('login'))
else:
messages.add_message(request, messages.INFO, "Only RSA keys are allowed.")
else:
return render(request, 'signup.html', {'form': form})
else:
form = RegistrationForm()
args = {'form': form}
return render(request, 'signup.html', args)