1👍
Update html to:
<thead class="table-light">
<tr>
<th scope="col">Total Equipment and Assets</th>
<th scope="col">
<!-- Value which is visible (calculated using JS) -->
<span id="Total"></span>
<!-- Add a hidden input to store value (value calculated and assigned using JS) -->
<input type="hidden" name="total_assets" value="0" id="total_assets">
</th>
</tr>
</thead>
Update script to assign resultGroupSet1
as:
- textual content to the span tag with
id=Total
- value to the hidden input with
name=total_assets
// Assign result to span tag which is visible
q('span#Total').textContent = resultGroupSet1;
// Assign result as value to hidden input field with name total_assets
q('input#total_assets').value = resultGroupSet1;
No other changes in views.
As an input field with a name="total_assets"
is used, the value will be passed on to the request body and will be accessible at request.POST
. Here, as the total_assets
field is hidden it is not visible to the users and still the value is available in POST data when form is submitted. So, when form.save()
is called the calculated value (using JS) will be saved.
0👍
I assumes your question is how to get the value in this element:
<th scope="col" id="Total"></th>
You can just simply add input element in your html code and add the name into it:
<th scope="col"><input id="Total" name="total_assets" value=""></th>
Then in your views.py:
def add_bp(request):
if request.method == 'POST':
form = infoForm(request.POST)
if form.is_valid():
form.save()
You can also manually get the Total:
def add_bp(request):
if request.method == 'POST':
total_assets = request.POST.get("total_assets")
- [Answered ]-Tried to import _mysql through MySQLdb and get the error
- [Answered ]-Python ImportError: No module named djng
- [Answered ]-NoReverseMatch at /
- [Answered ]-Query based on a composition of columns in Django
- [Answered ]-Can I use something like Hyde from within Django?
0👍
Probably what you are looking for is <input type="hidden" ...>
, it’s not visible by the end user and it’s included in the form submit
0👍
Why not do this calculation in Django Backend?
My suggestion is to pass all the arguments normally and just add a listener to the model saving (every time you will save an element to the table this little piece of code will run right before it saves it):
from django.db.models.signals import pre_save
from django.dispatch import receiver
@receiver(pre_save, model_class)
def form_pre_save(instance, *args, **kwargs):
instance.total_assets = instance.item_1_amount + instance.item_2_amount
This way when you want to save this kind of element in a different place (in the backend for example) you won’t have to re-write the code that does that, but rather just save the instance.
You can read more about the signals pre_save function here
- [Answered ]-Celery signatures .s(): early or late binding?
- [Answered ]-Run and communicate with background process in Django
- [Answered ]-403 template displaying instead of 404