Django获取绝对URL [英] Django Get Absolute URL
问题描述
http://domain.tld/article/post
但Django给了我只是
文章/帖子
我使用settings.py玩,但没有起作用。 (debug = false,允许主机vs。)
模板代码:
{%url'blog:detail'blog.slug%}
经过长时间的会见Django,我学到了很多东西。对于这个问题,我创建了一个绝对的URL模板标签。
将其添加到你的模板标签,然后使用默认的url标签:
{%absurl'some-view'with,arguments%}
绝对URL模板标签是Gist,您将需要向template_context_processors添加请求对象,否则这将无法正常工作。要实现此目的,请打开您的settings.py并添加以下行:
from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS +(
'django.core.context_processors.request',
)
I want to get absolute url in templates. I can't do with url. It gives me a relative URL. I need to get this:
http://domain.tld/article/post
but Django gives me just
/article/post
I played with settings.py but it didn't work. (debug=false, allowed hosts vs.)
Template code:
{% url 'blog:detail' blog.slug %}
After a long time meeting with Django, I learned a lot of things. For this issue, I created an absolute URL templatetag.
Add this to your template tags, then use like default url tag:
{% absurl 'some-view' with, arguments %}
Here is the Gist for the absolute URL templatetag, you will need to add request object to template_context_processors, otherwise this will not work. To achieve this, open your settings.py and add these following lines:
from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'django.core.context_processors.request',
)
这篇关于Django获取绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!