我想使我的注册页面上的字段为可选字段,以便仅当用户提供了这些信息时才检查该字段的内容,否则,只需跳过对数据完整性的检查即可:
pub pgp字段的pubpgp = PGPKey.from_blob(request.POST ['pubpgp']。rstrip(“ \ r \ n”))[0]
。但是我不知道该如何完成,欢迎您提供任何帮助。
models.py
pubpgp = models.TextField(verbose_name='Public RSA PGP Key - (ASCII-Armored)', blank=True, default='', max_length=3000)
views.py
def signup(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
pubpgp = PGPKey.from_blob(request.POST['pubpgp'].rstrip("\r\n"))[0]
if pubpgp.key_algorithm == PubKeyAlgorithm.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 based PGP keys are allowed.")
else:
return render(request, 'signup.html', {'form': form})
else:
form = RegistrationForm()
args = {'form': form}
return render(request, 'signup.html', args)
预先感谢
答案 0 :(得分:0)
由于如果未提供login
参数,或者如果提供了参数并指示正确的算法,则要重定向到pubpgp
视图,因此这应该包括对条件条件的细微调整声明:
def signup(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
pubpgp = request.POST.get('pubpgp')
# Allow redirect if pubpgp is empty or not empty but correct algorithm
if not pubpgp or PGPKey.from_blob(pubpgp.rstrip("\r\n"))[0].key_algorithm == PubKeyAlgorithm.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 based PGP keys are allowed.")
else:
return render(request, 'signup.html', {'form': form})
else:
form = RegistrationForm()
args = {'form': form}
return render(request, 'signup.html', args)