Django {{ MEDIA_URL }} 空白 @DEPRECATED

发布于 2024-09-24 02:47:17 字数 899 浏览 7 评论 0原文

在过去的几个小时里,我一直在为这个问题绞尽脑汁。 我无法让 {{ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深爱不及久伴 2024-10-01 02:47:17

您需要在 render_to_response 中添加 RequestContext 才能处理上下文处理器。

在您的情况下:

from django.template.context import RequestContext

context = {'latest': p}
render_to_response('Question/latest.html',
                   context_instance=RequestContext(request, context))

来自 docs

context_instance

上下文实例
来渲染模板。经过
默认情况下,将渲染模板
带有一个 Context 实例(填充有
字典中的值)。如果您需要
要使用上下文处理器,请渲染
带有 RequestContext 的模板
而是实例。

You need to add the RequestContext in your render_to_response for the context processors to be processed.

In your case:

from django.template.context import RequestContext

context = {'latest': p}
render_to_response('Question/latest.html',
                   context_instance=RequestContext(request, context))

From the docs:

context_instance

The context instance
to render the template with. By
default, the template will be rendered
with a Context instance (filled with
values from dictionary). If you need
to use context processors, render the
template with a RequestContext
instance instead.

眼藏柔 2024-10-01 02:47:17

添加媒体模板上下文处理器也可以完成工作

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
)

Adding media template context processor also gets the job done

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
)
浮光之海 2024-10-01 02:47:17

您还可以使用 direct_to_template:

from django.views.generic.simple import direct_to_template
...
return direct_to_template(request, 'Question/latest.html', {'latest': p})

You can also use direct_to_template:

from django.views.generic.simple import direct_to_template
...
return direct_to_template(request, 'Question/latest.html', {'latest': p})
始终不够 2024-10-01 02:47:17

除了上面提供的问题之外,建议您查看 photologue 应用程序。它可以帮助您避免模板文件中的直接链接并改用对象。
F.例如:

<img src="{{ artist.photo.get_face_photo_url }}" alt="{{ artist.photo.title }}"/>

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.:

<img src="{{ artist.photo.get_face_photo_url }}" alt="{{ artist.photo.title }}"/>
笑,眼淚并存 2024-10-01 02:47:17

更新:对于 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文