如何从Django模板中获取当前站点的域名?我试着在标签和过滤器中寻找,但没有什么。
当前回答
快速简单,但不适合生产:
(概览)
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的答案
其他回答
补充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,可以编写自己的例程。
正如@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引用客户端请求的主机,而不是代理主机。
from django.contrib.sites.models import Site
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
如果你使用“request”上下文处理器,并且正在使用Django sites框架,并且已经安装了Site中间件(即你的设置包括这些):
INSTALLED_APPS = [
...
"django.contrib.sites",
...
]
MIDDLEWARE = [
...
"django.contrib.sites.middleware.CurrentSiteMiddleware",
...
]
... 那么您将在模板中拥有可用的请求对象,并且它将包含对请求的当前Site的引用,即request. Site。然后你可以在模板中检索域:
{{request.site.domain}}
并附上网站名称:
{{request.site.name}}
我认为我们想要的是现有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...
推荐文章
- pylab和pyplot的区别是什么?
- Argparse:确定使用了哪个子解析器
- django导入错误-没有core.management模块
- 在芹菜中检索队列中的任务列表
- 使用beautifulsoup提取属性值
- 如何禁用标准错误流的日志记录?
- 用Matplotlib在Python中绘制时间
- 类中的Python装饰器
- 在Python中锁定文件
- 得到熊猫栏目的总数
- 从pandas DataFrame中删除名称包含特定字符串的列
- Mock vs MagicMock
- 如何阅读一个。xlsx文件使用熊猫库在iPython?
- 如何访问熊猫组由数据帧按键
- Pandas和NumPy+SciPy在Python中的区别是什么?