gpt4 book ai didi

Django {{ MEDIA_URL }} 空白 @DEPRECATED

转载 作者:行者123 更新时间:2023-11-28 19:36:09 24 4
gpt4 key购买 nike

在过去的几个小时里,我一直在思考这个问题。我无法让 {{ 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 显示为空白,我认为这应该是这样工作的,但也许我错了。

更新最新版本修复了这些问题。

最佳答案

您需要在 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

The context instanceto render the template with. Bydefault, the template will be renderedwith a Context instance (filled withvalues from dictionary). If you needto use context processors, render thetemplate with a RequestContextinstance instead.

关于Django {{ MEDIA_URL }} 空白 @DEPRECATED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3756841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com