在 Django 模板中添加两个变量?

NIR*_*RIA 2 python django templates

{% with a=pro_details.product_quantity|add:product_details.product_quantity %}
Run Code Online (Sandbox Code Playgroud)

我需要使用 with 和 add 在 Django 模板中添加两个变量。

小智 7

您可以为此使用自定义模板标签来实现。

templatetags/custom_tags.py 文件:

from django import template

register = template.Library()

@register.simple_tag
def add(a, b):
    return a+b
Run Code Online (Sandbox Code Playgroud)

模板部分,带有我们的标签调用:

{% load video_tags %}
Run Code Online (Sandbox Code Playgroud)

(你想使用的地方)

{% add 5 6 %}
Run Code Online (Sandbox Code Playgroud)

您也可以咨询此链接。https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/