Accessing request.META.SERVER_NAME in template

旧巷老猫 提交于 2019-12-11 05:26:49

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9073342/accessing-request-meta-server-name-in-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!