我有一个包含以下内容的模板:
示例1:
{% if player_mark %}
{% if commun.intern > threshold %}
<a class="btn btn-success btn-sm pull-right"
href="{% url 'games:map:town_map' map_id=map.id home_id=homecall.id %}"><i
class="fa fa-phone-square fa-fw"></i>
{{ title_header }}</a>
{% endif %}
{% endif %}
示例1上下文:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['player_mark'] = True
context['title_header'] = "This is map for home town"
return context
同样的陈述是复制和粘贴大约15次,并使它更整洁,我正在考虑有一个,它看起来像这样
示例2:
{% if {{ player_header }} %}
{% if commun.intern > threshold %}
<a class="btn btn-success btn-sm pull-right"
href="{% url '{{ url_header }}' map_id=map.id home_id=homecall.id %}"><i
class="fa fa-phone-square fa-fw"></i>
{{ title_header }}</a>
{% endif %}
{% endif %}
然后只需在我需要的内容中更新它
示例2上下文:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['player_header'] = ???
context['url_header'] = ???
context['title_header'] = "This is map for home town"
)
return context
我认为id能够将示例1中的if语句替换为示例2的内容,但后来我不知道如何在context_view中构造它。
任何想法都需要我知道。
这是我到目前为止在url上下文中的尝试:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['url_header'] = 'games:map:town_map' + "?{}{}".format(
map_id=self.map.id, ----- Unsure if this is correct way to pass url varibales
home_id=self) ----- Unsure if this is correct way to pass url varibales
context['header'] = "This is map for home town"
return context
希望这能让我更好地了解我在说什么。
解决方案HTML:
{% if button %}
{% if commun.intern > threshold %}
<a class="btn btn-success btn-sm pull-right"
href="{% url url_path map_id=map.id home_id=homecall.id %}"><i
class="fa fa-phone-square fa-fw"></i>
{{ header }}</a>
{% endif %}
{% endif %}
解决方案背景:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['button'] = True
context['url_path'] = 'games:map:town_map'
context['header'] = "This is map for home town"
return context
我如何解决问题,经过一些html阅读后我发现我因为括号{{}}而出错了。这些用于打印{%}}用于使用函数。