在模板中访问request.META.SERVER_NAME [英] Accessing request.META.SERVER_NAME in template

查看:208
本文介绍了在模板中访问request.META.SERVER_NAME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的角度来调用这样的模板:

  return render_to_response('mytemplate.html',context_instance = RequestContext请求))

我正在尝试访问我当前服务器的主机名(在这种情况下, localhost ),但是当我在模板中放置 {{request.META.SERVER_NAME}} 时,它打印为空白。 / p>

在我的 settings.py 文件中,我没有任何 TEMPLATE_CONTEXT_PROCESSORS 定义。我不知道我是否需要指定任何内容,否则可以解决问题。

解决方案

你必须添加请求上下文处理器将其自动添加到模板上下文中。或者你可以明确地将请求添加到上下文字典 render_to_response('foo',{'request':request})



https:// docs .djangoproject.com / en / dev / ref / templates / api /#django-core-context-processor-request



请注意,如果您添加请求上下文处理器,您应该记住添加默认值以及


I call a template like this from my view:

return render_to_response('mytemplate.html', context_instance=RequestContext(request))

I'm trying to access the hostname of my current server (in this case, localhost), but it just prints blank when I place {{request.META.SERVER_NAME}} in the template.

In my settings.py file, I don't have any TEMPLATE_CONTEXT_PROCESSORS defined. I'm not sure if I need to specify anything there, or if that could solve the problem.

解决方案

You have to add the request context processor to have it added to the template context automatically. Or you could explicitly add the request to the context dictionary render_to_response('foo', {'request': request})

https://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-request

Note that if you add the request context processor, you should remember to add the defaults as well.

这篇关于在模板中访问request.META.SERVER_NAME的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆