Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串计算为模板标记-Django_Django_Django Templates - Fatal编程技术网

将字符串计算为模板标记-Django

将字符串计算为模板标记-Django,django,django-templates,Django,Django Templates,我有一个Django模板,它包含一个模板标记,该标记接受一个变量(shop.id),并根据shop是否在数据库模型中返回两个字符串中的一个,如下所示 {% is_shop_claimed shop.id %} 模板标记返回的两个可能字符串是 return '<p>Taken</p>' return'take' 或 返回“” 运行代码时,如果返回第二个字符串,它将显示在模板中(在浏览器中查看页面源代码),如 并在浏览器中显示为如下链接 {% is_shop_cl

我有一个Django模板,它包含一个模板标记,该标记接受一个变量(shop.id),并根据shop是否在数据库模型中返回两个字符串中的一个,如下所示

{% is_shop_claimed shop.id %}
模板标记返回的两个可能字符串是

return '<p>Taken</p>'
return'take

'

返回“”
运行代码时,如果返回第二个字符串,它将显示在模板中(在浏览器中查看页面源代码),如


并在浏览器中显示为如下链接

{% is_shop_claimed shop.id %}
现在就去认领吧

问题是href中的shop.id没有被Django模板引擎计算为数字

例如,123号店的链接应该如下所示

<a href="/claim_shop/123/">Claim shop now</a>

我已经检查了Django文档中的过滤器,以便将其应用于模板标记或模板中的字符串,这样字符串就不会转义,但不会有好运

我已经看过了,但是似乎应该有一种简单的方法在模板中对{{shop.id}求值

我还使模板标记返回Bool,而不是两个字符串,将演示文稿保留在模板中,就像我更喜欢的那样,但在模板标记周围使用if语句,如下所示

{% if is_shop_claimed shop.id %}
    <p>Taken</p>
{% elif not is_shop_claimed shop.id %}
    <a href="/claim_shop/{{shop.id}}/">Claim shop now</a>
{% endif %}
{%if is_shop_claid shop.id%}
采取

{%elif不是\u shop\u claid shop.id%} {%endif%}
不起作用,因为我无法将模板标记放入if语句中

关于如何将{{shop.id}计算为一个数字,有什么建议吗?任何帮助都将不胜感激。
我正在学习Django和Python,我花了几个小时来解决这个问题。

您正在传递该值,所以只需替换它即可

return '<a href="/claim_shop/%s/">Claim shop now</a>' % (shop_id,) # or however you refer to it in the code
返回“”%(shop_id,)#或您在代码中引用它的方式

正在传递该值,因此只需将其替换为

return '<a href="/claim_shop/%s/">Claim shop now</a>' % (shop_id,) # or however you refer to it in the code
返回“”%(shop_id,)#或您在代码中引用它的方式

我建议您在您的店铺模型中添加一个声称为的属性:

class Shop(models.model):

    # you fields are here

    @property
    def is_claimed(self):
        # logik for determining if the shop is claimed
        if claimed:
            return True
        else:
            return False
然后您可以在模板中使用is:

{% if shop.is_claimed %}
    <p>Taken</p>
{% else %}
    <a href="/claim_shop/{{shop.id}}/">Claim shop now</a>
{% endif %}
{%if shop.is_索赔%}
采取

{%else%} {%endif%}

您甚至可以将其移动到一个片段中,您可以根据需要包含该片段,或者(更进一步)为其创建一个新的片段。

我建议您在您的店铺模型中添加一个
is\u claid
属性:

class Shop(models.model):

    # you fields are here

    @property
    def is_claimed(self):
        # logik for determining if the shop is claimed
        if claimed:
            return True
        else:
            return False
然后您可以在模板中使用is:

{% if shop.is_claimed %}
    <p>Taken</p>
{% else %}
    <a href="/claim_shop/{{shop.id}}/">Claim shop now</a>
{% endif %}
{%if shop.is_索赔%}
采取

{%else%} {%endif%}

您甚至可以将其移动到一个片段中,您可以根据需要将其包括在内,或者(更进一步)为其创建一个新的片段。

非常感谢您的好主意,我会尝试的!我没有尝试过这个解决方案,因为@Ignacio’s要简单得多,但再次感谢您的回答,也许这个解决方案对其他人来说很方便。非常感谢您的好主意,我会尝试的!我没有尝试过这个解决方案,因为@Ignacio的更简单,但再次感谢您的回答,也许这个解决方案对其他人来说会很方便。