'request.POST does not contain clicked button information
This is the code I have in forms.py
class RsaForm(forms.Form):
primeP = forms.IntegerField(label='Prime number (p)', required=True)
primeQ = forms.IntegerField(label='Prime number (q)', required=True)
clearText = forms.CharField(label='Cleartext', widget=forms.Textarea, required=False)
cipherText = forms.CharField(label='Ciphertext', widget=forms.Textarea, required=False)
And this is the code I have in views.py that I am using with my template rsa.html
def rsaView(request):
# if this is a POST request we need to process the form data
if request.is_ajax and request.method == 'POST':
# create a form instance and populate it with data from the request:
form = RsaForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
pParam = form.cleaned_data['primeP']
qParam = form.cleaned_data['primeQ']
cleartextParam = form.cleaned_data['clearText']
ciphertextParam = form.cleaned_data['cipherText']
print(request.POST)
pubkey, privkey = rsa.gen_keys(pParam, qParam)
if 'encryptinput' in request.POST:
# Encriptacion RSA
print('Encriptado.')
ciphertext = rsa.encrypt(cleartextParam, pubkey)
return JsonResponse({"ciphertext": ciphertext}, status=200)
elif 'decryptinput' in request.POST:
# Desencriptacion RSA
print('Desencriptado.')
cleartext = rsa.decrypt(ciphertextParam, privkey)
return JsonResponse({"cleartext": cleartext}, status=200)
else:
return JsonResponse({"error": "Hubo un error."}, status=200)
else:
print("Invalid form.")
# if a GET (or any other method) we'll create a blank form
else:
form = RsaForm()
thisCryptosystem = Cryptosystem.objects.get(name="RSA")
template = loader.get_template('cryptogyapp/rsa.html')
context = {
'thisCryptosystem': thisCryptosystem,
'form': form
}
return HttpResponse(template.render(context, request))
And this is my rsa.html
{% extends "cryptogyapp/base.html" %}
{% block title %}{{ thisCryptosystem.name }} Cryptosystem{% endblock %}
{% block title-header %}{{ thisCryptosystem.name }} Cryptosystem{% endblock %}
{% block subtitle-header %}{{ thisCryptosystem.desc }}{% endblock %}
{% block content %}
<form id="rsaForm" method="post">
{% csrf_token %}
<div class="row gx-5 justify-content-center">
<div class="col-lg-6">
<div class="form-floating mb-3">
<input name="{{form.primeP.name}}" class="form-control" id="primeq" type="text" placeholder="Enter a prime number...">
<label name="primeP">Prime number (p)</label>
</div>
<div class="form-floating mb-3">
<input name="{{form.primeQ.name}}" class="form-control" id="primep" type="text" placeholder="Enter a prime number...">
<label name="primeQ">Prime number (q)</label>
</div>
<div class="form-floating mb-3">
<textarea name="{{form.clearText.name}}" class="form-control" id="cleartextarea" type="text" placeholder="Enter a cleartext..."></textarea>
<label name="primeP">Cleartext</label>
</div>
<div class="form-floating mb-3">
<textarea name="{{form.cipherText.name}}" class="form-control" id="ciphertextarea" type="text" placeholder="Enter a ciphertext..."></textarea>
<label name="primeP">Ciphertext</label>
</div>
<input class="btn btn-primary" type="submit" value="Encrypt" name="encryptinput"/>
<input class="btn btn-primary" type="submit" value="Decrypt" name="decryptinput"/>
</div>
</div>
</form>
{% endblock content %}
Whenever I click either Encrypt button or Decrypt button, the information is not passed in request.POST, the only thing request.POST shows me is the info from the form fields, but not the button that was clicked.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
