如何从Django模板中获取当前站点的域名?我试着在标签和过滤器中寻找,但没有什么。


我认为你想要的是访问请求上下文,参见RequestContext。


如果您想要实际的HTTP Host报头,请参阅Daniel Roseman对@Phsiao的回答的评论。另一种选择是使用contrib。sites框架,你可以在数据库中为站点设置一个规范的域名(将请求域映射到一个具有正确SITE_ID的设置文件是你必须通过你的webserver设置自己做的事情)。在这种情况下,你要寻找:

from django.contrib.sites.models import Site

current_site = Site.objects.get_current()
current_site.domain

如果您想使用current_site对象,则必须自己将其放入模板上下文中。如果你到处都在使用它,你可以把它打包到一个模板上下文处理器中。


补充Carl Meyer,你可以做一个这样的上下文处理器:

module.context_processors.py

from django.conf import settings

def site(request):
    return {'SITE_URL': settings.SITE_URL}

当地settings.py

SITE_URL = 'http://google.com' # this will reduce the Sites framework db call.

settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    "module.context_processors.site",
    ....
 )

返回上下文实例的模板,url站点为{{SITE_URL}}

如果想在上下文处理器中处理子域或SSL,可以编写自己的例程。


我使用的上下文处理器的变化是:

from django.contrib.sites.shortcuts import get_current_site
from django.utils.functional import SimpleLazyObject


def site(request):
    return {
        'site': SimpleLazyObject(lambda: get_current_site(request)),
    }

SimpleLazyObject包装器确保仅在模板实际使用站点对象时才发生DB调用。这将从管理页面中删除查询。它还缓存结果。

并将其包含在设置中:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    "module.context_processors.site",
    ....
)

在模板中,可以使用{{site。Domain}}获取当前域名。

编辑:也支持协议切换,使用:

def site(request):
    site = SimpleLazyObject(lambda: get_current_site(request))
    protocol = 'https' if request.is_secure() else 'http'

    return {
        'site': site,
        'site_root': SimpleLazyObject(lambda: "{0}://{1}".format(protocol, site.domain)),
    }

我发现了{{请求。Get_host}}方法。


您可以在模板中使用{{protocol}}://{{domain}}来获取您的域名。


我使用自定义模板标记。例如:<your_app>/templatetags/site.py:

# -*- coding: utf-8 -*-
from django import template
from django.contrib.sites.models import Site

register = template.Library()

@register.simple_tag
def current_domain():
    return 'http://%s' % Site.objects.get_current().domain

在模板中使用它,就像这样:

{% load site %}
{% current_domain %}

from django.contrib.sites.models import Site
if Site._meta.installed:
    site = Site.objects.get_current()
else:
    site = RequestSite(request)

类似于用户panchicore的回复,这是我在一个非常简单的网站上所做的。 它提供了一些变量,并使它们在模板中可用。

SITE_URL将保存类似example.com的值 SITE_PROTOCOL将保存类似http或https的值 SITE_PROTOCOL_URL将包含http://example.com或https://example.com这样的值 SITE_PROTOCOL_RELATIVE_URL将包含类似//example.com的值。

模块/ context_processors.py

from django.conf import settings

def site(request):

    SITE_PROTOCOL_RELATIVE_URL = '//' + settings.SITE_URL

    SITE_PROTOCOL = 'http'
    if request.is_secure():
        SITE_PROTOCOL = 'https'

    SITE_PROTOCOL_URL = SITE_PROTOCOL + '://' + settings.SITE_URL

    return {
        'SITE_URL': settings.SITE_URL,
        'SITE_PROTOCOL': SITE_PROTOCOL,
        'SITE_PROTOCOL_URL': SITE_PROTOCOL_URL,
        'SITE_PROTOCOL_RELATIVE_URL': SITE_PROTOCOL_RELATIVE_URL
    }

settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    "module.context_processors.site",
    ....
 )

SITE_URL = 'example.com'

然后,在你的模板上,使用它们作为{{SITE_URL}}, {{SITE_PROTOCOL}}, {{SITE_PROTOCOL_URL}}和{{SITE_PROTOCOL_RELATIVE_URL}}


快速简单,但不适合生产:

(概览)

    request.scheme               # http or https
    request.META['HTTP_HOST']    # example.com
    request.path                 # /some/content/1/

(在模板中)

{{ request.scheme }} :// {{ request.META.HTTP_HOST }} {{ request.path }}

确保使用RequestContext,如果你使用渲染就是这样。

不要相信请求。生产中的META['HTTP_HOST']:该信息来自浏览器。相反,请使用@CarlMeyer的答案


我知道这个问题很老了,但我是在寻找一种python方法来获取当前域时偶然发现的。

def myview(request):
    domain = request.build_absolute_uri('/')[:-1]
    # that will build the complete domain: http://foobar.com

{{请求。get_host}}在和ALLOWED_HOSTS设置(在Django 1.4.4中添加)一起使用时,可以防止HTTP主机头攻击。

注意{{request.META。HTTP_HOST}}没有相同的保护。查看文档:

ALLOWED_HOSTS A list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations. ... If the Host header (or X-Forwarded-Host if USE_X_FORWARDED_HOST is enabled) does not match any value in this list, the django.http.HttpRequest.get_host() method will raise SuspiciousOperation. ... This validation only applies via get_host(); if your code accesses the Host header directly from request.META you are bypassing this security protection.


至于在模板中使用请求,在Django 1.8中,模板渲染函数调用已经改变了,所以你不再需要直接处理RequestContext。

下面是如何渲染视图的模板,使用快捷函数render():

from django.shortcuts import render

def my_view(request):
    ...
    return render(request, 'my_template.html', context)

下面是如何呈现一个电子邮件模板,IMO是更常见的情况下,你想要的主机值:

from django.template.loader import render_to_string

def my_view(request):
    ...
    email_body = render_to_string(
        'my_template.txt', context, request=request)

下面是一个在电子邮件模板中添加完整URL的示例;请求。Scheme应该获得HTTP或HTTPS,这取决于您使用的是什么:

Thanks for registering! Here's your activation link:
{{ request.scheme }}://{{ request.get_host }}{% url 'registration_activate' activation_key %}

这种方法怎么样?对我有用。 它也用于django注册。

def get_request_root_url(self):
    scheme = 'https' if self.request.is_secure() else 'http'
    site = get_current_site(self.request)
    return '%s://%s' % (scheme, site)

在Django模板中,你可以这样做:

<a href="{{ request.scheme }}://{{ request.META.HTTP_HOST }}{{ request.path }}?{{ request.GET.urlencode }}" >link</a>

如果你使用“request”上下文处理器,并且正在使用Django sites框架,并且已经安装了Site中间件(即你的设置包括这些):

INSTALLED_APPS = [
    ...
    "django.contrib.sites",
    ...
]

MIDDLEWARE = [
    ...
     "django.contrib.sites.middleware.CurrentSiteMiddleware",
    ...
]

... 那么您将在模板中拥有可用的请求对象,并且它将包含对请求的当前Site的引用,即request. Site。然后你可以在模板中检索域:

    {{request.site.domain}}

并附上网站名称:

    {{request.site.name}}

你可以使用request.build_absolute_uri()

默认情况下,它将返回一个完整的路径。

但是如果你传入一个像这样的参数:

request.build_absolute_uri('/')

这将返回域名。


我认为我们想要的是现有url标签的替代品,所以我写了一个新标签:

from django.template import Library
from django.urls     import reverse

@register.simple_tag(takes_context = True)
def fullURL(context, name, *args, **kwargs):
    request = context['request']
    return f'{request.scheme}://{request.get_host()}{reverse(name, args = args, kwargs = kwargs)}'

然后在你的模板中你可以这样…

{% extends "myapp/email/email_base.html" %}

{% load mytags %} {# Replace mytags with whatever the name of your custom tags calss is. #}

{% block content %}
<p>You can use <a href="{% fullURL 'signup' %}">this link</a> to get started with your account. We look forward to seeing you soon!</p>
{% endblock content %}

然后,当你生成这个时,你只需要记住将请求传递到上下文,就像这样……

from django.template.loader import render_to_string

def sendEmail(subject, to, template, **context):
    html = render_to_string(f'myapp/email/{template}.html', context | {'subject': subject})
    # ... and so on with the rest of my function for sending email...

正如@furins的回复中提到的,代理服务器可能存在问题。我在使用Apache和uWSGI - request时发现了这一点。Get_host或request。Build_absolute_uri将返回代理主机(127.0.0.1:9191…)。

然而,有人已经发布了一个有用的指南来解决这个问题:

https://ubuntu.com/blog/django-behind-a-proxy-fixing-absolute-urls

虽然这是一个相对较老的答案,但它仍然与django 3.2和python 3.9相关。

为了防止这个答案在未来消失,以下是它的要点:

settings.py

# Setup support for proxy headers
USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

apache.conf

<VirtualHost *:443>
    ...
    RequestHeader set X-Forwarded-Proto 'https' env=HTTPS

    ProxyPass / http://10.0.0.3/
    ProxyPassReverse / http://10.0.0.3/
    ...
</VirtualHost>

使用这些设置请求。Get_host和request。Build_absolute_uri引用客户端请求的主机,而不是代理主机。


下面这些可以获得完整的url和部分url:

def myview(request):
    request.build_absolute_uri()
    # http://localhost:8000/admin/store/product/

    request.build_absolute_uri('/')
    # http://localhost:8000/

    request.build_absolute_uri('/')[:-1]
    # http://localhost:8000

    request.scheme
    # http

    request.META['HTTP_HOST']
    # localhost:8000

    request.path    
    # /admin/store/product/