小编典典

Django {{MEDIA_URL}}空白@DEPRECATED

django

在过去的几个小时中,我一直对此不以为然。我无法显示{{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显示为空白,我以为这是假设的工作方式,但也许我错了。


阅读 413

收藏
2020-03-31

共1个答案

小编典典

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

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
)
2020-03-31