“Django”;加上;模板中

“Django”;加上;模板中,django,django-template-filters,Django,Django Template Filters,我遇到了一个模板,点击数据太多次的问题。我知道我可以使用“with”来避免这种情况发生,但我不知道如何使用“with”。 这是我在模板中的代码 <table class="calendar1"> <tr> {% for month, game in one_month %} {% for day in month %} <td> {{ day }} <br />

我遇到了一个模板,点击数据太多次的问题。我知道我可以使用“with”来避免这种情况发生,但我不知道如何使用“with”。 这是我在模板中的代码

<table class="calendar1">
<tr>
    {% for month, game in one_month %}
        {% for day in month %}

                <td>
            {{ day }} <br />
            {% if forloop.parentloop.counter == 1 and day == 1 %}New Year's Day{% endif %}
            {% if forloop.parentloop.first and day == 16 %}Martin Luther King, Jr. Day Observance.{% endif %}
            {% if forloop.parentloop.counter == 1 or forloop.parentloop.counter == 11 or forloop.parentloop.counter == 12 %}No game today{% endif %}
            {% if forloop.parentloop.counter == 2 and day < 25 %}No game today{% endif %}
            {% if forloop.parentloop.counter == 10 and day > 1 %}No game today{% endif %}
                {% if forloop.parentloop.counter == 2 %}
                    {% if day == 14 %}Pitchers and catchers report & physicals{% endif %}
                    {% if day == 15 %}Pitchers and catchers first workout{% endif %}
                    {% if day == 17 %}Position players report & physicals{% endif %}
                    {% if day == 18 %}First full squad workout{% endif %}
                    {% for game in mlbgames %}
                        {% if day == game.date.day and game.date.month == 2 %}
                            {{ game.away_team }} {% if game.away_team == "Angels" %} @ {% else %} vs. {% endif %} {{ game.home_team }} {{ game.date }} 

                        {% endif %}
                    {% endfor %}
                {% endif %}
                {% if forloop.parentloop.counter == 3 %}
                {% if day == 7 or day == 20 %}No game today{% endif %}
                    {% for game in mlbgames %}

                        {% if day == game.date.day and game.date.month == 3 %}
                            {{ game.away_team }} {% if game.away_team == "Angels" %} @ {% else %} vs. {% endif %}  {{ game.home_team }} {{ game.date }} 

                        {% endif %}
                    {% endfor %}
                {% endif %}{% if forloop.parentloop.counter == 4 %}
                {% if day == 2 or day == 10 %}No game today{% endif %}
                    {% for game in mlbgames %}

                        {% if day == game.date.day and game.date.month == 4 %}
                            {{ game.away_team }} {% if game.away_team == "Angels" %} @ {% else %} vs. {% endif %}  {{ game.home_team }} {{ game.date }} 
                        {% endif %}
                    {% endfor %}
                {% endif %}{% if forloop.parentloop.counter == 5 %}
                {% if day == 1 or day == 18 %}No game today{% endif %}
                    {% for game in mlbgames %}

                        {% if day == game.date.day and game.date.month == 5 %}
                            {{ game.away_team }} {% if game.away_team == "Angels" %} @ {% else %} vs. {% endif %}  {{ game.home_team }} {{ game.date }} 
                        {% endif %}
                    {% endfor %}
                {% endif %}{% if forloop.parentloop.counter == 6 %}
                {% if day == 5 or day == 19 %}No game today{% endif %}
                    {% for game in mlbgames %}

                        {% if day == game.date.day and game.date.month == 6 %}
                            {{ game.away_team }} {% if game.away_team == "Angels" %} @ {% else %} vs. {% endif %}  {{ game.home_team }} {{ game.date }} 
                        {% endif %}
                    {% endfor %}
                {% endif %}{% if forloop.parentloop.counter == 7 %}
                {% if day == 10 or day == 11 or day == 12 or day == 13 %}All-stars game break{% endif %}
                {% if day == 6 or day == 20 or day == 17 or day == 24 %}No game today{% endif %}
                    {% for game in mlbgames %}
                        {% if day == game.date.day and game.date.month == 7 %}
                            {{ game.away_team }} {% if game.away_team == "Angels" %} @ {% else %} vs. {% endif %}  {{ game.home_team }} {{ game.date }} 
                        {% endif %}
                    {% endfor %}
                {% endif %}{% if forloop.parentloop.counter == 8 %}
                    {% for game in mlbgames %}
                        {% if day == game.date.day and game.date.month == 8 %}
                            {{ game.away_team }} {% if game.away_team == "Angels" %} @ {% else %} vs. {% endif %}  {{ game.home_team }} {{ game.date }} 
                        {% endif %}
                    {% endfor %}
                {% endif %}{% if forloop.parentloop.counter == 9 %}
                    {% for game in mlbgames %}
                        {% if day == game.date.day and game.date.month == 9 %}
                            {{ game.away_team }} {% if game.away_team == "Angels" %} @ {% else %} vs. {% endif %}  {{ game.home_team }} {{ game.date }} 
                        {% endif %}
                    {% endfor %}
                {% endif %}{% if forloop.parentloop.counter == 10 %}
                    {% for game in mlbgames %}
                        {% if day == game.date.day and game.date.month == 10 %}
                            {{ game.away_team }} {% if game.away_team == "Angels" %} @ {% else %} vs. {% endif %}  {{ game.home_team }} {{ game.date }} 
                        {% endif %}
                    {% endfor %}
                {% endif %}

            </td>

        {% if forloop.last %}
        </tr>
        {% else %}
            {% if forloop.counter|divisibleby:"7" %}
                </tr><tr>
            {% endif %}
        {% endif %}
        {% endfor %}
    {% endfor %}
</table>

{月份为%,一个月内游戏%}
{月份%中的天的%}
{{day}}
{%if-forloop.parentloop.counter==1,day==1%}新年{%endif%} {%if-forloop.parentloop.first and day==16%}小马丁·路德·金纪念日。{%endif%} {%if-forloop.parentloop.counter==1或forloop.parentloop.counter==11或forloop.parentloop.counter==12%}今天没有比赛{%endif%} {%if-forloop.parentloop.counter==2且日<25%}今天没有比赛{%endif%} {%if-forloop.parentloop.counter==10天>1%}今天没有比赛{%endif%} {%if-forloop.parentloop.counter==2%} {%if-day==14%}投手和捕手报告和体检{%endif%} {%if-day==15%}投手和接球手第一次训练{%endif%} {%if-day==17%}位置球员报告和体检{%endif%} {%if-day==18%}第一次全队训练{%endif%} {mlbgames%中游戏的% {%if day==game.date.day和game.date.month==2%} {game.away\u team}{%if game.away\u team==“天使”%}{%else%}vs.{%endif%}{{game.home\u team}{{{game.date} {%endif%} {%endfor%} {%endif%} {%if-forloop.parentloop.counter==3%} {%if day==7或day==20%}今天没有比赛{%endif%} {mlbgames%中游戏的% {%if day==game.date.day和game.date.month==3%} {game.away\u team}{%if game.away\u team==“天使”%}{%else%}vs.{%endif%}{{game.home\u team}{{{game.date} {%endif%} {%endfor%} {%endif%}{%if forloop.parentloop.counter==4%} {%if day==2或day==10%}今天没有比赛{%endif%} {mlbgames%中游戏的% {%if day==game.date.day和game.date.month==4%} {game.away\u team}{%if game.away\u team==“天使”%}{%else%}vs.{%endif%}{{game.home\u team}{{{game.date} {%endif%} {%endfor%} {%endif%}{%if forloop.parentloop.counter==5%} {%if day==1或day==18%}今天没有比赛{%endif%} {mlbgames%中游戏的% {%if day==game.date.day和game.date.month==5%} {game.away\u team}{%if game.away\u team==“天使”%}{%else%}vs.{%endif%}{{game.home\u team}{{{game.date} {%endif%} {%endfor%} {%endif%}{%if forloop.parentloop.counter==6%} {%if day==5或day==19%}今天没有比赛{%endif%} {mlbgames%中游戏的% {%if day==game.date.day和game.date.month==6%} {game.away\u team}{%if game.away\u team==“天使”%}{%else%}vs.{%endif%}{{game.home\u team}{{{game.date} {%endif%} {%endfor%} {%endif%}{%if forloop.parentloop.counter==7%} {%if-day==10或day==11或day==12或day==13%}全明星赛休息时间{%endif%} {%if day==6或day==20或day==17或day==24%}今天没有比赛{%endif%} {mlbgames%中游戏的% {%if day==game.date.day和game.date.month==7%} {game.away\u team}{%if game.away\u team==“天使”%}{%else%}vs.{%endif%}{{game.home\u team}{{{game.date} {%endif%} {%endfor%} {%endif%}{%if forloop.parentloop.counter==8%} {mlbgames%中游戏的% {%if day==game.date.day和game.date.month==8%} {game.away\u team}{%if game.away\u team==“天使”%}{%else%}vs.{%endif%}{{game.home\u team}{{{game.date} {%endif%} {%endfor%} {%endif%}{%if forloop.parentloop.counter==9%} {mlbgames%中游戏的% {%if day==game.date.day和game.date.month==9%} {game.away\u team}{%if game.away\u team==“天使”%}{%else%}vs.{%endif%}{{game.home\u team}{{{game.date} {%endif%} {%endfor%} {%endif%}{%if forloop.parentloop.counter==10%} {mlbgames%中游戏的% {%if day==game.date.day和game.date.month==10%} {game.away\u team}{%if game.away\u team==“天使”%}{%else%}vs.{%endif%}{{game.home\u team}{{{game.date} {%endif%} {%endfor%} {%endif%} {%if-forloop.last%} {%else%} {%if-forloop.counter |可除数为:“7”} {%endif%} {%endif%} {%endfor%} {%endfor%}
问题在于mlbgames变量包含一个棒球队的所有比赛。我将在我的循环中度过一个月,检查这个月是否有游戏。然后当循环结束时,我开始从第一个游戏开始的游戏列表,但我想
{% with value=assignment %}
   The value: {{ value }}
{% endwith %}
{% with game_day=game.date.day %}  
    {{ game_day }} # Now game_day holds the game.date.day
{% endwith %}
{% with game_month=game.date.month %}   
    {{ game_month }}  # Now game_month holds the game.date.month
{% endwith %}
{% with away_team=game.away_team %}  
    {{ away_team }}    # Now away_team holds the game.away_team
{% endwith %}
{% with counter=forloop.parentloop.counter %}   
    {{ counter }} # Also a lot of hits so I included this
{% endwith %}