django blocktrans和tag-DRY进近

django blocktrans和tag-DRY进近,django,django-templates,Django,Django Templates,如何使用下面的代码段创建一个干式方法 这可能是一个简单的问题,但它让我感到困惑 我已经试了两天了,但我不知道如何在将django与tag合并时只使用一个blocktrans 以下是我的模板标记代码: {% if user.userprofile.subscription_category == subscription_type_free %} {% with temp_max_language_versions=max_language_versions_free_subscripti

如何使用下面的代码段创建一个干式方法

这可能是一个简单的问题,但它让我感到困惑

我已经试了两天了,但我不知道如何在将django与tag合并时只使用一个blocktrans

以下是我的模板标记代码:

{% if user.userprofile.subscription_category == subscription_type_free %}
    {% with temp_max_language_versions=max_language_versions_free_subscription %}
        {% blocktrans %}You have the ability to create <strong>{{ temp_max_language_versions }}</strong> different {{ resume_details_tran }} with your <a href="{{ user_account_details }}">subscription</a>.{% endblocktrans %}
    {% endwith %}

{% elif user.userprofile.subscription_category == subscription_type_03 %}
    {% with temp_max_language_versions=max_language_versions_level03_subscription %}
    {% blocktrans %}You have the ability to create <strong>{{ temp_max_language_versions }}</strong> different {{ resume_details_tran }} with your <a href="{{ user_account_details }}">subscription</a>.{% endblocktrans %}
    {% endwith %}

{% elif user.userprofile.subscription_category == subscription_type_06 %}
    {% with temp_max_language_versions=max_language_versions_level06_subscription %}
        {% blocktrans %}You have the ability to create <strong>{{ temp_max_language_versions }}</strong> different {{ resume_details_tran }} with your <a href="{{ user_account_details }}">subscription</a>.{% endblocktrans %}
    {% endwith %}

{% elif user.userprofile.subscription_category == subscription_type_12 %}
   {% with temp_max_language_versions=max_language_versions_level12_subscription %}
       {% blocktrans %}You have the ability to create <strong>{{ temp_max_language_versions }}</strong> different {{ resume_details_tran }} with your <a href="{{ user_account_details }}">subscription</a>.{% endblocktrans %}
   {% endwith %}

{% endif %}
{%if user.userprofile.subscription\u category==subscription\u type\u free%}
{具有临时\u最大\u语言\u版本的%=最大\u语言\u版本\u免费\u订阅%}
{%blocktrans%}您可以使用您的{%endblocktrans%}创建{{temp\u max\u language\u versions}不同的{{resume\u details\u tran}}}
{%endwith%}
{%elif user.userprofile.subscription\u category==subscription\u type\u 03%}
{具有临时\u最大\u语言\u版本的百分比=最大\u语言\u版本\u级别03\u订阅%}
{%blocktrans%}您可以使用您的{%endblocktrans%}创建{{temp\u max\u language\u versions}不同的{{resume\u details\u tran}}}
{%endwith%}
{%elif user.userprofile.subscription\u category==subscription\u type\u 06%}
{具有临时\u最大\u语言\u版本的百分比=最大\u语言\u版本\u级别06\u订阅%}
{%blocktrans%}您可以使用您的{%endblocktrans%}创建{{temp\u max\u language\u versions}不同的{{resume\u details\u tran}}}
{%endwith%}
{%elif user.userprofile.subscription\u category==subscription\u type\u 12%}
{具有临时\u最大\u语言\u版本的百分比=最大\u语言\u版本\u级别12\u订阅%}
{%blocktrans%}您可以使用您的{%endblocktrans%}创建{{temp\u max\u language\u versions}不同的{{resume\u details\u tran}}}
{%endwith%}
{%endif%}

使用不同的temp\u max\u language\u版本变量,您的HTML始终是相同的。解决这一问题的三种方法:

1-使用一个包括:

{% if user.userprofile.subscription_category == subscription_type_free %}
    {% include 'path_to_include.html' with  temp_max_language_versions=max_language_versions_free_subscription %}
{% elif user.userprofile.subscription_category == subscription_type_03 %}
....
2-或者您可以将其添加到类别本身;一些伪代码:

class SubscriptionCategory(...):

    @property
    def temp_max_language_versions(self):
         if self.type == 'something':
              return 'something_else'

3-如果它是特定于视图的,您也可以通过视图将其添加到类别中。

谢谢弗朗索瓦-我使用了include。