Django {{ MEDIA_URL }} 空白 @DEPRECATED
在过去的几个小时里,我一直在为这个问题绞尽脑汁。 我无法让 {{ MEDIA_URL }} 显示
在 settings.py 中
..
MEDIA_URL = 'http://10.10.0.106/ame/'
..
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.media",
)
..
在我看来,
from django.shortcuts import render_to_response, get_object_or_404
from ame.Question.models import Question
def latest(request):
Question_latest_ten = Question.objects.all().order_by('pub_date')[:10]
p = get_object_or_404(Question_latest_ten)
return render_to_response('Question/latest.html', {'latest': p})
,然后我有一个 base.html 和 Question/latest.html
{% extends 'base.html' %}
<img class="hl" src="{{ MEDIA_URL }}/images/avatar.jpg" /></a>
但 MEDIA_URL 显示为空白,我认为这就是它的工作方式,但是也许我错了。
更新 最新版本修复了这些问题。
I have banged my head over this for the last few hours.
I can not get {{ MEDIA_URL }} to show up
in settings.py
..
MEDIA_URL = 'http://10.10.0.106/ame/'
..
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.media",
)
..
in my view i have
from django.shortcuts import render_to_response, get_object_or_404
from ame.Question.models import Question
def latest(request):
Question_latest_ten = Question.objects.all().order_by('pub_date')[:10]
p = get_object_or_404(Question_latest_ten)
return render_to_response('Question/latest.html', {'latest': p})
then i have a base.html and Question/latest.html
{% extends 'base.html' %}
<img class="hl" src="{{ MEDIA_URL }}/images/avatar.jpg" /></a>
but MEDIA_URL shows up blank, i thought this is how its suppose to work but maybe I am wrong.
Update
Latest version fixes these problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在
render_to_response
中添加RequestContext
才能处理上下文处理器。在您的情况下:
来自 docs:
You need to add the
RequestContext
in yourrender_to_response
for the context processors to be processed.In your case:
From the docs:
添加媒体模板上下文处理器也可以完成工作
Adding media template context processor also gets the job done
您还可以使用 direct_to_template:
You can also use direct_to_template:
除了上面提供的问题之外,建议您查看 photologue 应用程序。它可以帮助您避免模板文件中的直接链接并改用对象。
F.例如:
In addition to question provided above can suggest you to take a look at photologue application. It could help you to avoid direct links in template files and use objects instead.
F.ex.:
更新:对于 Django 1.10 用户,媒体和静态上下文处理器都已从 django.core 移至 django.template 中,请阅读以下文章以获取更多信息:https://docs.djangoproject.com/en/1.10/ref/templates/api/#django-template -上下文处理器媒体
Update: For Django 1.10 users, the both media and static context processors are already moved in django.template from django.core read the following article for more info: https://docs.djangoproject.com/en/1.10/ref/templates/api/#django-template-context-processors-media