如何在Django模板中进行简单的算术计算器?

发布于 2025-02-12 19:50:40 字数 1123 浏览 1 评论 0原文

我想从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的往事 2025-02-19 19:50:40

通用方式

模板不是为数据的任何计算或修改而设计的。它仅用于以优选的方式显示数据。

由于所有模板都需要在Django中的视图,因此只需将计算放在视图中即可。

views.py

def your_view(request, *args, **kwargs):
    # ... your code
    vis = visits.objects.filter(patient=patid)
    for a in vis:
        a.calculated_value = make_calculation(a)
    context = {'vis': vis}
    return render(request, 'your_template.html', context)

your_template.html

{% for a in vis %}
  {{ a.numone }} {{ a.numtwo }} {{ a.calculated_value }}
{% endfor %}

如果计算值来自同一模型实例,则来自同一模型对象,您只需添加方法或属性对您的模型并在模板中使用它。

model.py

class ModelA(models.Model):
    # ... your code
    @property
    def calculated_value(self):
        return (self.numone + self.numtwo * 2) / 25

your_template.html

{% for a in vis %}
  {{ a.numone }} {{ a.numtwo }} {{ a.calculated_value }}
{% en

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

def your_view(request, *args, **kwargs):
    # ... your code
    vis = visits.objects.filter(patient=patid)
    for a in vis:
        a.calculated_value = make_calculation(a)
    context = {'vis': vis}
    return render(request, 'your_template.html', context)

your_template.html

{% for a in vis %}
  {{ a.numone }} {{ a.numtwo }} {{ a.calculated_value }}
{% endfor %}

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

class ModelA(models.Model):
    # ... your code
    @property
    def calculated_value(self):
        return (self.numone + self.numtwo * 2) / 25

your_template.html

{% for a in vis %}
  {{ a.numone }} {{ a.numtwo }} {{ a.calculated_value }}
{% en
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文