如何在Django模板中进行简单的算术计算器?
我想从Django模板中的模型显示两组数字。我正在使用循环获取表中的每一行数据以在模板中显示如下:
{in in vis%}
{a.numone} {a.numtwo}
{%endfor%}
它的工作正常!
现在,我想使用此数字进行简单的算术计算。例如,说例如添加,sub,div,乘以这两个数字并在模板中显示。
怎么做?
问题以下更新:
model.py
class visits(models.Model):
doctor = models.ForeignKey(doctorprofile, on_delete=models.CASCADE)
patient = models.ForeignKey(patientprofile, on_delete=models.CASCADE)
visit_cons_fee = models.DecimalField(null=False, blank=False, max_digits=10, decimal_places=2)
visit_other_fee = models.DecimalField(null=True, blank=True, max_digits=10, decimal_places=2)
def total_fee(self):
return (self.visit_cons_fee + self.visit_other_fee)
def __str__ (self):
return str(self.id)
views.py
vis=visits.objects.filter(patient=patid).values( 'visit_cons_fee','visit_other_fee', 'doctor__doc_Fname', 'doctor__doc_Lname' )
模板如下:
{%vis%}
咨询费:{%如果a.visit_cons_fee%} {{a.visit_cons_fee}} }
其他费用:{如果a.visit_other_fee %} {{a.visit_other_fee}}} {%else%} 0 {%endif%}
总费用:????
{%endfor%}
I want to show two sets of number from model in the django template for the same. I am using for loop to get each row data of the table to show in template as below :
{% for a in vis %}
{a.numone}
{a.numtwo}
{% endfor %}
its working fine!.
Now, I want to do simple arithmetic calculation by using this numbers. say for example add, sub, div, multiply this two number and show it in template.
How to do it?
Question Updated below:
Model.py
class visits(models.Model):
doctor = models.ForeignKey(doctorprofile, on_delete=models.CASCADE)
patient = models.ForeignKey(patientprofile, on_delete=models.CASCADE)
visit_cons_fee = models.DecimalField(null=False, blank=False, max_digits=10, decimal_places=2)
visit_other_fee = models.DecimalField(null=True, blank=True, max_digits=10, decimal_places=2)
def total_fee(self):
return (self.visit_cons_fee + self.visit_other_fee)
def __str__ (self):
return str(self.id)
views.py
vis=visits.objects.filter(patient=patid).values( 'visit_cons_fee','visit_other_fee', 'doctor__doc_Fname', 'doctor__doc_Lname' )
template as below:
{% for a in vis %}
Consultation Fees : {% if a.visit_cons_fee %}{{a.visit_cons_fee}}{% else %} 0 {% endif %}
Other Fees : {% if a.visit_other_fee %}{{a.visit_other_fee}}{% else %} 0 {% endif %}
Total Fees : ????
{% endfor %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通用方式
模板不是为数据的任何计算或修改而设计的。它仅用于以优选的方式显示数据。
由于所有模板都需要在Django中的视图,因此只需将计算放在视图中即可。
views.py
your_template.html
值
如果计算值来自同一模型实例,则来自同一模型对象,您只需添加方法或属性对您的模型并在模板中使用它。
model.py
your_template.html
Generic way
The template is not designed for any calculation or modification of data. It is used just for showing the data in the prefered way.
As all template need a view in django, just place the calculation in the view instead.
views.py
your_template.html
Values are coming from the same model object
If the values for the calculation are coming from the same model instance, you could just add a method or property to your model and use this in your template.
models.py
your_template.html