我怎么能得到完整/绝对的URL(例如https://example.com/some/path)在Django没有网站模块?这太愚蠢了……我不需要查询我的数据库来抓取URL!
我想使用reverse()。
我怎么能得到完整/绝对的URL(例如https://example.com/some/path)在Django没有网站模块?这太愚蠢了……我不需要查询我的数据库来抓取URL!
我想使用reverse()。
当前回答
你还可以使用:
import socket
socket.gethostname()
这对我来说很好,
我不太清楚它是怎么运作的。我相信这是更低级的,它将返回您的服务器主机名,这可能与您的用户访问您的页面所使用的主机名不同。
其他回答
你也可以使用get_current_site作为sites应用程序的一部分(from django.contrib.sites.models import get_current_site)。它接受一个请求对象,如果request为None,则默认为您在settings.py中使用SITE_ID配置的站点对象。阅读更多使用站点框架的文档
e.g.
from django.contrib.sites.shortcuts import get_current_site
request = None
full_url = ''.join(['http://', get_current_site(request).domain, obj.get_absolute_url()])
它不像request.build_absolute_url()那样紧凑/整洁,但是当请求对象不可用时,并且您有一个默认的站点url时,它是可用的。
在你看来,只需这样做:
base_url = "{0}://{1}{2}".format(request.scheme, request.get_host(), request.path)
如果你不能访问request,那么你就不能像这里的一些解决方案中推荐的那样使用get_current_site(request)。您可以使用原生Sites框架和get_absolute_url的组合。在管理中设置至少一个站点,确保你的模型有一个get_absolute_url()方法,然后:
>>> from django.contrib.sites.models import Site
>>> domain = Site.objects.get_current().domain
>>> obj = MyModel.objects.get(id=3)
>>> path = obj.get_absolute_url()
>>> url = 'http://{domain}{path}'.format(domain=domain, path=path)
>>> print(url)
'http://example.com/mymodel/objects/3/'
https://docs.djangoproject.com/en/dev/ref/contrib/sites/#getting-the-current-domain-for-full-urls
正如在其他答案中提到的,request.build_absolute_uri()是完美的,如果你可以访问request,而sites框架是伟大的,只要不同的url指向不同的数据库。
然而,我的用例略有不同。我的登台服务器和生产服务器访问相同的数据库,但是get_current_site都返回数据库中的第一个站点。要解决这个问题,必须使用某种环境变量。你可以使用1)一个环境变量(类似os.environ。get('SITE_URL', 'localhost:8000'))或2)不同的site_id为不同的服务器和不同的设置。
希望有人会觉得这有用!
如果你不想访问数据库,你可以通过设置来实现。然后,使用上下文处理器将其添加到每个模板:
# settings.py (Django < 1.9)
...
BASE_URL = 'http://example.com'
TEMPLATE_CONTEXT_PROCESSORS = (
...
'myapp.context_processors.extra_context',
)
# settings.py (Django >= 1.9)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
# Additional
'myapp.context_processors.extra_context',
],
},
},
]
# myapp/context_processors.py
from django.conf import settings
def extra_context(request):
return {'base_url': settings.BASE_URL}
# my_template.html
<p>Base url is {{ base_url }}.</p>