[英]template url reversal escaping surt arguments
I'm having an issue where the template url reversal is escaping colon and parenthetical characters. 我遇到一个问题,其中模板网址反向转义了冒号和括号字符。 I want these characters to remain unescaped in the anchor tag's href attribute.
我希望这些字符在锚标记的href属性中保持不转义 。 It used to behave this way when I was in django 1.3, but upgrading to 1.6, I notice that this does not behave as I want.
在django 1.3中时,它曾经以这种方式运行,但是在升级到1.6时,我注意到这并没有达到我想要的状态。
What I have: 我有的:
surt = 'http://(gov/'
browse_domain = 'gov'
... in template ...
<a href="{% url 'nomination.views.url_surt' project.project_slug surt %}">{{ browse_domain }}</a>
This yields: 这样产生:
<a href="/nomination/eth2008/surt/http%3A//%28gov/">gov</a>
As you can see, the colon :
and left parenthetical (
characters are being escaped in the url href attribute. I don't want that. 如您所见,冒号
:
和左括号(
字符在url href属性中进行了转义。我不希望这样。
What I want: 我想要的是:
surt = 'http://(gov/'
browse_domain = 'Gov'
... in template ...
<a href="{% url 'nomination.views.url_surt' project.project_slug surt %}">{{ browse_domain }}</a>
This yields: 这样产生:
<a href="/nomination/eth2008/surt/http://(gov/">gov</a>
Anyone know how to keep these characters from escaping when I'm reversing URLs in my anchor tag? 反向定位锚标记中的URL时,有人知道如何防止这些字符转义吗?
NOTE: The below answer is wrong. 注意:以下答案是错误的。 urllib.quote(safe=':()') will indeed keep those safe characters unescaped.
urllib.quote(safe =':()')确实会使那些安全字符不转义。 Something else is happening in django to cause this problem and I still don't know where it is.
django中正在发生其他事情,导致此问题,但我仍然不知道它在哪里。
In Django 1.6, any url reversal in the template will first pass through iri_to_uri()
before it is rendered to HTML. 在Django 1.6中,模板中的所有url反转都必须先通过
iri_to_uri()
传递,然后再呈现为HTML。 There is no override for this in the template call to url reverse {% url %}
as-is. 按原样对url反向
{% url %}
的模板调用中对此没有覆盖。
Notice this bit of italicized text detailing the change. 请注意此斜体文本详细说明了更改。
This is iri_to_uri()
这是
iri_to_uri()
def iri_to_uri(iri):
"""
Convert an Internationalized Resource Identifier (IRI) portion to a URI
portion that is suitable for inclusion in a URL.
This is the algorithm from section 3.1 of RFC 3987. However, since we are
assuming input is either UTF-8 or unicode already, we can simplify things a
little from the full method.
Returns an ASCII string containing the encoded result.
"""
# The list of safe characters here is constructed from the "reserved" and
# "unreserved" characters specified in sections 2.2 and 2.3 of RFC 3986:
# reserved = gen-delims / sub-delims
# gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
# sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
# / "*" / "+" / "," / ";" / "="
# unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
# Of the unreserved characters, urllib.quote already considers all but
# the ~ safe.
# The % character is also added to the list of safe characters here, as the
# end of section 3.1 of RFC 3987 specifically mentions that % must not be
# converted.
if iri is None:
return iri
return urllib.quote(smart_str(iri), safe="/#%[]=:;$&()+,!?*@'~")
At first glance, this might look like :
, (
, and )
are safe from escaped hex-encoding because they are passed as 'safe' to urllib.quote()
: 乍看起来,这看起来像
:
, (
和)
urllib.quote()
转义的十六进制编码,因为它们作为“安全”传递给urllib.quote()
:
_safe_map = {}
for i, c in zip(xrange(256), str(bytearray(xrange(256)))):
_safe_map[c] = c if (i < 128 and c in always_safe) else '%{:02X}'.format(i)
_safe_quoters = {}
def quote(s, safe='/'):
# fastpath
if not s:
if s is None:
raise TypeError('None object cannot be quoted')
return s
cachekey = (safe, always_safe)
try:
(quoter, safe) = _safe_quoters[cachekey]
except KeyError:
safe_map = _safe_map.copy()
safe_map.update([(c, c) for c in safe])
quoter = safe_map.__getitem__
safe = always_safe + safe
_safe_quoters[cachekey] = (quoter, safe)
if not s.rstrip(safe):
return s
return ''.join(map(quoter, s))
If you step through the actual urllib.quote()
method as shown above, 'safe' actually means that those characters will be escaped/quoted . 如果逐步执行上述实际的
urllib.quote()
方法,则“安全”实际上意味着这些字符将被转义/引用 。 Initially, I thought 'safe' meant 'safe-from-quoting'. 最初,我认为“安全”的意思是“报价安全”。 It caused me a great deal of confusion.
这使我感到非常困惑。 I guess they instead mean, 'safe' as 'safe-in-terms-of-sections-2.2-and-2.3-of-RFC-3986'.
我想它们的意思是“安全”,如“ RFC-3986中2.2-2.3节的安全性”。 Perhaps a more elaborately named keyword argument would be prudent, but then again, there's a whole cornucopia of things I find awkward regarding
urllib
. 也许更精心命名的关键字参数是审慎的,但是话又说回来,关于
urllib
我发现有些事情令人尴尬。 ಠ_ಠ ಠ_ಠ
After much research, and due to the fact that we don't want to modify Django core methods, our team decided to do some hacky url-construction in the template (the very kind Django docs strongly eschew ). 经过大量研究,并且由于我们不想修改Django核心方法,我们的团队决定在模板中进行一些复杂的url构造(非常友好的Django文档eschew )。 It's not perfect, but it works for our use case.
它不是完美的,但适用于我们的用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.