我必须分配给一个Django项目。我需要知道URL,http:// .... 由于使用'urls.py',我们的确有'原始'信息。我如何了解完整的URL名称;与Django URL详细信息
http+domain+parameters
阿米特的意思。
我必须分配给一个Django项目。我需要知道URL,http:// .... 由于使用'urls.py',我们的确有'原始'信息。我如何了解完整的URL名称;与Django URL详细信息
http+domain+parameters
阿米特的意思。
HTTPS状态将由不同的Web服务器以不同方式处理。
对于我的Nginx反向代理到Apache + WSGI设置,我明确地设置了一个头部,apache(django)可以检查连接是否安全。
此信息在URL中不可用,但在视图请求对象中不可用。
django使用request.is_secure()
来确定连接是否安全。它如何实现取决于后端。
http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.is_secure
例如,对于mod_python的,这是下面的代码:
def is_secure(self):
try:
return self._req.is_https()
except AttributeError:
# mod_python < 3.2.10 doesn't have req.is_https().
return self._req.subprocess_env.get('HTTPS', '').lower() in ('on', '1')
如果您使用的是代理,你可能会发现它有用的HTTP头中HttpRequest.META
可用http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
更新:如果要记录每个安全请求,请使用上述示例与中间件
class LogHttpsMiddleware(object):
def process_request(self, request):
if request.is_secure():
protocol = 'https'
else:
protocol = 'http'
print "%s://www.mydomain.com%s" % (protocol, request.path)
添加LogHttpsMiddleware
您settings.py
MIDDLEWARE_CLASSES
我不知道你们提供的信息是否对我有帮助,因为我已经告诉过你,我只是Django的新手。无论如何,我再次解释我需要它。我的应用程序使用特定的URL(http:// ....)访问网络服务器,我不知道它从数据库获取数据。我想打印这些网址的地方说,最好在命令提示符,如默认日志'runserver' 07/Feb/2011 19:13:42]“GET/devices/acer/HTTP/1.1”200 11595 希望你能理解我的需要。 – 2011-02-07 13:56:43
我完全理解你在说什么。使用`request.is_secure()`来确定您的后端是否检测到连接是安全的(如在`HTTPS`中)。如果您想打印每个安全请求,请使用中间件。我将用一个可打印所有安全请求的中间件示例进行更新。 – 2011-02-07 14:00:23
看看这个片断:
http://djangosnippets.org/snippets/1197/
我修改了它这样的:
from django.contrib.sites.models import RequestSite
from django.contrib.sites.models import Site
def site_info(request):
site_info = {'protocol': request.is_secure() and 'https' or 'http'}
if Site._meta.installed:
site_info['domain'] = Site.objects.get_current().domain
site_info['name'] = Site.objects.get_current().name
else:
site_info['domain'] = RequestSite(request).domain
site_info['name'] = RequestSite(request).name
site_info['root'] = site_info['protocol'] + '://' + site_info['domain']
return {'site_info':site_info}
的的if/else是因为不同版本的Django Site API
这个片段实际上是一个上下文处理器,所以你必须将其粘贴到您的应用程序调用context_processors.py
文件,然后添加到您的设置:
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
'name-of-your-app.context_processors.site_info',
)
的+
在这里照顾我们ð 'ont覆盖由django设置的可能的默认上下文处理器,现在或将来,我们只是将这一个添加到元组中。
最后,确保在返回响应时您的视图中使用RequestContext
,而不仅仅是Context
。这解释了here in the docs。
这只是一个使用的物质:
def some_view(request):
# ...
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
你说的是在您的网站生成一个URL到指定的页面? – 2011-02-07 12:37:24