简体   繁体   English

Django 模板标签可以像 Django 模板调用一样使用吗?

[英]Can Django template tags be used like Django Template callables?

I'm using the the Django Template Language to call into a model's method to create dynamic ids according to product-specific logic.我正在使用 Django 模板语言调用模型的方法来根据产品特定的逻辑创建动态 ID。

JSON files hold product-specific configuration information. JSON 文件包含产品特定的配置信息。 I use the Django Template Language in strings in the JSON files so each product has their own logic for creating product-specific ids.我在 JSON 文件的字符串中使用 Django 模板语言,因此每个产品都有自己的逻辑来创建产品特定的 ID。 It works great with a Django Template Language callables.它适用于 Django 模板语言可调用对象。

The id_template has a callable to the to a model's method get_next_policy_id: id_template 可以调用模型的方法 get_next_policy_id:

{
    "model": "some name",
    "product_id": "some product name",
    "id_template": "{{ policy.get_next_product_id }}"
}

Here's the relevant code from the product model.这是来自产品 model 的相关代码。 You can see the method that provides the id, and the save method conditionally uses it.可以看到提供 id 的方法,save 方法有条件地使用它。

class Policy():
    [...]
    def get_next_policy_id_risk_strategies(self):
        policy_num = -1
        sequence_name = self.product.product_id + '-' + self.source_application.form["state"]
        if self.source_application.form["state"] == "NY":
            policy_num = get_next_value(sequence_name, initial_value=200_000)

        elif self.source_application.form["state"] == "IA":
            policy_num = get_next_value(sequence_name, initial_value=400_000)
        else:
            sequence_name = self.product.product_id
            policy_num = get_next_value(sequence_name, initial_value=300_000)

        policy_id = self.product.product_id + "-" + str(policy_num)
        return policy_id

That works great.这很好用。 However, I'd like to have no product-specific code in the model.但是,我希望 model 中没有特定于产品的代码。 I could do this almost entirely in the Django Template Language, but the requirements for the product id, is they vary by state sequentially.我几乎可以完全在 Django 模板语言中做到这一点,但是对产品 ID 的要求是按顺序随 state 变化。 So, get_next_value draws from a persisted sequence in the database.因此,get_next_value 从数据库中的持久序列中提取。 Thus, I'd like to move the conditional logic to the JSON file.因此,我想将条件逻辑移至 JSON 文件。

However, callables take no parameters.但是,可调用对象不带参数。 And I need to pass the start of the sequence and the state as parameters to generate the id.我需要传递序列的开头和 state 作为参数来生成 id。 Since tags can take parameters, I thought to use simple tags, which can take parameters.由于标签可以带参数,我想使用简单的标签,它可以带参数。 Along with template language conditionals.连同模板语言条件。 I thought I should be able to have a template string like:我想我应该能够有一个模板字符串,如:

{
   "id_template": "{{ product.product_id }}-{% if product.source_application.form.state == \"NY\" %}{{ product.get_next_product_id "NY" 200000 }}{% elif product.source_application.form.state == \"IA\" %}{{ product.get_next_product_id "IA" 400000 }}{% else %}{{ product.get_next_product_id "other" 300000 }}{% endif %}"
}

Then, in the model, I'd place the following code:然后,在 model 中,我将放置以下代码:

    @register.simple_tag(name="get_next_product_id")
    def get_next_product_id(self, state, initial_value):
        sequence_name = self.product.product_id + "-" + self.source_application.form["state"]
        return get_next_value(sequence_name, initial_value=initial_value)

Unfortunately, I get an error from this:不幸的是,我从中得到一个错误:

E           django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: ' "NY" 200000' from 'get_next_product_id "NY" 200000'

I thought simple tags could have arguments.我认为简单的标签可以有 arguments。 Is that mistaken?这是误会了吗?

Because the template string is not being parsed, I simplified the string to the following:因为没有解析模板字符串,所以我将字符串简化为以下内容:

{
   "id_template": "{{ product.get_next_product_id "NY" 200000 }}"
}

But the error remains the same.但错误仍然是一样的。 Removing the quotes around "NY" has no effect.删除“NY”周围的引号没有任何效果。

The question is:问题是:

Is what I am trying to do using Django Template Tags possible?我正在尝试使用 Django 模板标签做什么? And, if not, is there another way I should be moving the product-specific logic out of the model?而且,如果没有,是否有另一种方法可以将特定于产品的逻辑移出 model?

No that is not at all how it works.不,这根本不是它的工作原理。 A template tag cannot be a method on a model.模板标签不能是 model 上的方法。 If you want to define a template tag, it needs to go in a file inside the templatetags directory of your app, and you need to call it with the tag syntax {%... %} .如果你想定义一个模板标签,它需要 go 在你的应用程序的templatetags目录下的一个文件中,你需要使用标签语法{%... %}来调用它。 Of course because of that it can't accept self , you would need to pass the product explicitly.当然因为它不能接受self ,你需要明确地传递产品。

So, in for example templatetags/my_tags.py:因此,例如在 templatetags/my_tags.py 中:

@register.simple_tag(name="get_next_product_id")
def get_next_product_id(product, state, initial_value):
    sequence_name = product.product_id + "-" + product.source_application.form["state"]
    return get_next_value(sequence_name, initial_value=initial_value)

then in the template:然后在模板中:

{% load my_tags %}
...
{% get_next_product_id product "NY" 200000 %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM