如何在django模板中将名称反转为绝对url?

如何在django模板中将名称反转为绝对url?,django,templates,url,Django,Templates,Url,{%url\u name%}给出了一个相对名称 如何执行类似于{%absolute\u url\u name%}的操作,使其返回带基的url(包括端口,如果存在的话)?有不同的解决方案。编写您自己的templatetag并使用HttpRequest.build\u absolute\u uri(位置)。但另一种方式,有点老套 <a href="{{ request.scheme }}://{{ request.get_host }}{% url url_name %}"

{%url\u name%}
给出了一个相对名称


如何执行类似于
{%absolute\u url\u name%}
的操作,使其返回带基的url(包括端口,如果存在的话)?

有不同的解决方案。编写您自己的templatetag并使用
HttpRequest.build\u absolute\u uri(位置)
。但另一种方式,有点老套

<a href="{{ request.scheme }}://{{ request.get_host }}{% url url_name %}">click here</a>
示例:

<a href='{% abs_url view_name obj.uuid %}'>

{% url view_name obj.uuid as view_url %}
<a href='{{ view_url|as_abs_url:request }}'>

{%url视图\名称obj.uuid作为视图\ url%}

您可以在请求对象中使用
build\u absolute\u uri()
方法。在模板中,将其用作请求。生成绝对uri。这将创建绝对地址,包括协议、主机和端口

例如:

 <a href="{{request.build_absolute_uri}}">click here</a>

在模板中,我使用它打印带有协议、主机和端口(如果存在)的绝对URL:

<a href="{{ request.scheme }}://{{ request.get_host }}{% url url_name %}">link</a>

我使用一个自定义标记
absurl
和上下文处理器
django.template.context\u processors.request
。例如:

tenplatetags\mytags.py
中定义的自定义标记:

from django import template
register = template.Library()

@register.simple_tag(takes_context=True)
def absurl(context, object):
    return context['request'].build_absolute_uri(object.get_absolute_url())
settings.py
中,确保您具有以下功能:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'OPTIONS': {
        'debug': DEBUG,
        'context_processors': [
            ...
            'django.template.context_processors.request',
            ...
然后确保模型有一个
build\u absolute\u url
,例如供管理区域使用:

class ProductSelection(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    fixed_price = models.DecimalField(max_digits=10, decimal_places=2, ...
    ...

    def get_absolute_url(self):
        return reverse('myapp:selection', args=[self.slug])
模板本身中的使用
absurl
将其绑定在一起:

 {% load mytags %}
 ...
 <script type="application/ld+json">{
    "@context": "http://schema.org/",
    "@type": "Product",
    "name": " {{selection.title}}",
    ...
    "offers": {
      "@type": "Offer",
      "priceCurrency": "GBP",
      "url": "{% absurl selection %}",      <--- look here
    } }
  </script>
{%loadmytags%}
...
{
“@context”:”http://schema.org/",
“@type”:“产品”,
“名称”:“{selection.title}}”,
...
“提议”:{
“@type”:“Offer”,
“价格货币”:“英镑”,

url:“{%absurl selection%}”,是否有一种方法可以将协议包括在其中?应该可以帮助您解决这个问题。@igo下面的回答也涵盖了协议在2020年,没有更好的方法了?@dangel,我完全同意您的看法。我一直想知道为什么这会让我获得这么多的支持票。用一些可能的答案更新了答案。仅一个代码块并不能提供一个好的答案。P请添加解释(为什么它解决了问题,错误在哪里等…)访问模板中的HttpRequest.build_absolute_uri()方法。这将比{{request.get_host}{%url url_name%}简单易行只有当您只对请求的路径感兴趣,而不是对任意的
url\u名称
感兴趣时,这才有效。这对命名url没有任何作用,但它会为元标记提供所需的url
og:url
。正如我在回答中所解释的,在python中,您可以使用:
请求。构建绝对uri(反向('url\u name',args=(一些arg1,)
是否可以在模板中执行此操作?
 {% load mytags %}
 ...
 <script type="application/ld+json">{
    "@context": "http://schema.org/",
    "@type": "Product",
    "name": " {{selection.title}}",
    ...
    "offers": {
      "@type": "Offer",
      "priceCurrency": "GBP",
      "url": "{% absurl selection %}",      <--- look here
    } }
  </script>