33

ここ数時間、私はこれについて頭を悩ませてきました。{{ 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 は空白で表示されます。これが機能すると思われますが、間違っているかもしれません。

最新バージョンに更新 すると、これらの問題が修正されます。

4

5 に答える 5

30

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.

于 2010-09-21T02:12:29.810 に答える
2

direct_to_templateを使用することもできます。

from django.views.generic.simple import direct_to_template
...
return direct_to_template(request, 'Question/latest.html', {'latest': p})
于 2010-09-21T13:02:06.753 に答える
2

上記の質問に加えて、photologueアプリケーションを検討することをお勧めします。テンプレート ファイル内の直接リンクを避け、代わりにオブジェクトを使用すると役立つ場合があります。例:

<img src="{{ artist.photo.get_face_photo_url }}" alt="{{ artist.photo.title }}"/>
于 2010-09-21T14:00:00.130 に答える