Django保存字段

时间:2018-12-28 14:16:26

标签: python django view

如果用户未提供任何有关此字段的信息,则

im当前正尝试保存一个不包含任何值的字段(pubpgp)(可选)。 但是由于某些原因,如果不提供字段“ pubpgp”的信息,我将无法保存实际表格... 该如何解决?

def edit_profile(request):
    if request.method == 'POST':
        form = UserForm(request.POST, instance=request.user)
        try:
            pubpgp = PGPKey.from_blob(request.POST['pubpgp'].rstrip("\r\n"))[0]
        except:
            messages.add_message(request, messages.INFO,"PGP-Key is wrong formated.")
            return render(request, 'edit_profile.html', {'form': form})
        if pubpgp.key_algorithm == PubKeyAlgorithm.RSAEncryptOrSign:
            form.save()
            messages.add_message(request, messages.INFO, "Profile has been updated successfully.")
            return redirect(reverse('home'))
        else:
            messages.add_message(request, messages.INFO, "PGP-Key is wrong formated.")
            return render(request, 'edit_profile.html', {'form': form})
    else:
        form = UserForm(instance=request.user)
        args = {'form': form}
        return render(request, 'edit_profile.html', args)

models.py

pubpgp = models.TextField(blank=True, null=True, max_length=3000, unique=True)

0 个答案:

没有答案