'how to pass context value to specific input over the form.as_table in django template
i need to pass value from my view to specific label in the my form.as_table in this case look at live output template i want assign t_v_a as context to t_v_a as quittanceForm.as_table
view.py
def Tva_Calculate(request):
form_data=forms.QuittanceRegister(request.POST or None)
t_v_a=0
if form_data.is_valid():
t_v_a=form_data.cleaned_data['loyer_acteul_411000']*9/100
context ={
'quittanceForm': form_data,
't_v_a':t_v_a
}
return render(request,'quittance.html',context)
template.py
<center>
<form action="" method="POST">
<h1>Register new Quittance</h1>
{% csrf_token %}
<table border="1">
{{quittanceForm.as_table}}
</table>
<button type="submit">Register now</button>
<button type="submit">Calculate_tva</button>
</form>
{{t_v_a}}
</center>
live output template enter image description here
Solution 1:[1]
The best approach is to render your form manually. It gives you more freedom.
Solution 2:[2]
class formModel(forms.ModelForm):
class Meta:
model = yourModel
fields = ['fields here']
widgets = {
'tva_attribute':forms.TextInput(attrs={'value': YOUR_TVA_VALUE}),
}
Try this hopefully it will work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bunya |
| Solution 2 | Danish Shirsangi |
