问题 如何在django模板中表示“{{”?


我正在尝试在Django中以bibtex格式输出,模板看起来像这样:

@{{ pubentry.type }{,
  author    = {{% for author in pubentry.authors.all %}{{ author.first_name }} {{ author.middle_name }} {{ author.last_name }}{% if not forloop.last %} and {% endif %}
              {% endfor %}},
  title     = {{{ pubentry.title }}},
  journal   = {{{ pubentry.journal }}}
}

问题在于 {{{ 要么 {{%。解决问题的一种方法是在第一个之后添加一个空格 {,但那种篡改格式。什么是正确的逃生方式 { 在Django模板?


9128
2018-05-08 16:44


起源



答案:


看看吧 templatetag 标签:

输出用于组成模板标签的语法字符之一。

由于模板系统有 没有“逃避”的概念,要显示模板标签中使用的其中一个位,必须使用 {% templatetag %} 标签。

你所追求的是:

{% templatetag openvariable %}

也许有一个更好的解决方案,因为这不会增加可读性......


12
2018-05-08 17:02





另一种(更灵活的)方法可能是在将值发送到模板之前将值转换为类似bibtex的值。你可能还需要这样做以逃避bibtex / latex无法处理的一些字符。这是我之前准备的类似的东西:

import datetime

class BibTeXString(unicode):
  pass

def bibtex_repr(obj):
  """ A version of the string repr method, that always outputs variables suitable for BibTeX. """
  # If this has already been processed, it's ok
  if isinstance(obj, BibTeXString):
    return obj
  # Translate strings
  if isinstance(obj, basestring):
    value = unicode(obj).translate(CHAR_ESCAPES).strip()
    return BibTeXString('{%s}' % value)
  # Dates
  elif isinstance(obj, datetime.date):
    return BibTeXString('{%02d-%02d-%02d}' % (obj.year, obj.month, obj.day))
  # Integers
  if isinstance(obj, (int, long)):
    return BibTeXString(str(obj))
  else:
    return BibTeXString(repr(obj))


CHAR_ESCAPES = {
  ord(u'$'): u'\\$',
  ord(u'&'): u'\\&',
  ord(u'%'): u'\\%',
  ord(u'#'): u'\\#',
  ord(u'_'): u'\\_',
  ord(u'\u2018'): u'`',
  ord(u'\u2019'): u"'", 
  ord(u'\u201c'): u"``", 
  ord(u'\u201d'): u"''" ,
  ord(u'\u2014'): u'---', 
  ord(u'\u2013'): u'--',
}

如果需要,您甚至可以将其用作模板过滤器,使模板看起来像:

@{{ pubentry.type }{,
  author    = {% filter bibtex %}{% for author in pubentry.authors.all %}{{ author.first_name }} {{ author.middle_name }} {{ author.last_name }}{% if not forloop.last %} and {% endif %}{% endfor %}}{% endfilter %},
  title     = {{ pubentry.title|bibtex }},
  journal   = {{ pubentry.journal|bibtex }}
}

但是我会在它到达模板之前逃避内容,因此您的模板只需要这样做:

@{{ pubentry.type }{,
  {% for field in fields %}{{ field }}{% if not forloop.last %},{% endif %}{% endfor %}
}

或者甚至在这个阶段完全省略模板。祝你好运!


3
2018-05-08 22:42



谢谢!听起来很有意思 - 我还在学习Django / Python(到目前为止8小时的exp),但我会研究这个。 - rxin


答案:


看看吧 templatetag 标签:

输出用于组成模板标签的语法字符之一。

由于模板系统有 没有“逃避”的概念,要显示模板标签中使用的其中一个位,必须使用 {% templatetag %} 标签。

你所追求的是:

{% templatetag openvariable %}

也许有一个更好的解决方案,因为这不会增加可读性......


12
2018-05-08 17:02





另一种(更灵活的)方法可能是在将值发送到模板之前将值转换为类似bibtex的值。你可能还需要这样做以逃避bibtex / latex无法处理的一些字符。这是我之前准备的类似的东西:

import datetime

class BibTeXString(unicode):
  pass

def bibtex_repr(obj):
  """ A version of the string repr method, that always outputs variables suitable for BibTeX. """
  # If this has already been processed, it's ok
  if isinstance(obj, BibTeXString):
    return obj
  # Translate strings
  if isinstance(obj, basestring):
    value = unicode(obj).translate(CHAR_ESCAPES).strip()
    return BibTeXString('{%s}' % value)
  # Dates
  elif isinstance(obj, datetime.date):
    return BibTeXString('{%02d-%02d-%02d}' % (obj.year, obj.month, obj.day))
  # Integers
  if isinstance(obj, (int, long)):
    return BibTeXString(str(obj))
  else:
    return BibTeXString(repr(obj))


CHAR_ESCAPES = {
  ord(u'$'): u'\\$',
  ord(u'&'): u'\\&',
  ord(u'%'): u'\\%',
  ord(u'#'): u'\\#',
  ord(u'_'): u'\\_',
  ord(u'\u2018'): u'`',
  ord(u'\u2019'): u"'", 
  ord(u'\u201c'): u"``", 
  ord(u'\u201d'): u"''" ,
  ord(u'\u2014'): u'---', 
  ord(u'\u2013'): u'--',
}

如果需要,您甚至可以将其用作模板过滤器,使模板看起来像:

@{{ pubentry.type }{,
  author    = {% filter bibtex %}{% for author in pubentry.authors.all %}{{ author.first_name }} {{ author.middle_name }} {{ author.last_name }}{% if not forloop.last %} and {% endif %}{% endfor %}}{% endfilter %},
  title     = {{ pubentry.title|bibtex }},
  journal   = {{ pubentry.journal|bibtex }}
}

但是我会在它到达模板之前逃避内容,因此您的模板只需要这样做:

@{{ pubentry.type }{,
  {% for field in fields %}{{ field }}{% if not forloop.last %},{% endif %}{% endfor %}
}

或者甚至在这个阶段完全省略模板。祝你好运!


3
2018-05-08 22:42



谢谢!听起来很有意思 - 我还在学习Django / Python(到目前为止8小时的exp),但我会研究这个。 - rxin


随着 templatetag 模板标签。

title     = {% templatetag openvariable %}{% templatetag openbrace %} pubentry.title {% templatetag closevariable %}{% templatetag closebrace %},

1
2018-05-08 17:03