Html Django 模板上的 URL 编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14547491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:17:27  来源:igfitidea点击:

URL encoding on Django Template

htmldjangodjango-templatesurl-encoding

提问by jdtoh

I have this anchor link:

我有这个锚链接:

<a href="/question/tag/1/1/?list_id={{title}}">{{title}}</a>

Sometimes, this title has some content with + (add operator) like: "Django + Python"

有时,这个标题有一些带有 +(添加运算符)的内容,例如:“Django + Python”

But when it is directly placed on anchor links, the url delivered will be:

但是当它直接放在锚链接上时,传递的 url 将是:

http://127.0.0.1:8080/question/tag/1/1/?list_id=Django + Python

Which will eventually cause problem on the retrieval as the url decoder thought the list_id GET = DjangoPython.

这最终会导致检索问题,因为 url 解码器认为 list_id GET = DjangoPython。

So, does anyone knows how to avoid this issue? Note that I do not want to change anchor links to input buttons.

那么,有谁知道如何避免这个问题?请注意,我不想将锚链接更改为输入按钮。

回答by jdtoh

Instead of

代替

{{ title }}

do

{{title|urlencode}}

回答by trojjer

Instead of hardcoding the URL and building the querystring in the template by concatenating the title context item, consider simplifying it a bit by using django.core.urlresolvers.reverse() and specifying a named URL from the relevant urls.py file. Makes for a clean design when you don't have to worry about URLs changing! Imagine the headache of all the scattered HREFs you'd have to change, versus simply assigning a context variable from the view set to the result of the reverse call. The querystring can be added nicely in the view too, without having to worry about some of the often strange template parsing rules. Note that the urllib.urlencode function acts completely differently to the template filter of the same name; don't get confused! It can be used as follows:

与其硬编码 URL 并通过连接标题上下文项在模板中构建查询字符串,不如考虑使用 django.core.urlresolvers.reverse() 并从相关 urls.py 文件中指定命名 URL 来简化它。当您不必担心 URL 更改时,可以实现干净的设计!想象一下您必须更改所有分散的 HREF 的头痛,而不是简单地将视图集的上下文变量分配给反向调用的结果。查询字符串也可以很好地添加到视图中,而不必担心一些通常很奇怪的模板解析规则。请注意,urllib.urlencode 函数的作用与同名模板过滤器完全不同;不要混淆!它可以按如下方式使用:

# If the entry in URLConf has named capture groups such as ?<pk>,
# pass a dict as kwargs parameter instead.
url = reverse('your-named-url', args=(1, 1))  # e.g '/question/tag/1/1/'
# Encodes as form data to 'list_id=Django+%2B+Python'
querystring = urllib.urlencode({'list_id': 'Django + Python'})
context['question_url'] = '{}?{}'.format(url, querystring)

Then, in the template it can be as simple as href="{{ question_url }}". It might seem like more work, but it can pay off very quickly and it's a much better separation of concerns. Normally I'd use the {% url "your-named-url" %}template tag, but it doesn't currently handle querystrings nicely -- as in, not in the same manner as it deals with URL args and kwargs defined in URLConf.

然后,在模板中它可以像href="{{ question_url }}". 这似乎需要更多的工作,但它可以很快得到回报,而且它是一种更好的关注点分离。通常我会使用{% url "your-named-url" %}模板标签,但它目前不能很好地处理查询字符串——就像它处理 URLConf 中定义的 URL args 和 kwargs 的方式不同。