相关疑难解决方法(0)

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",
)
..
Run Code Online (Sandbox Code Playgroud)

在我看来,我有

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})
Run Code Online (Sandbox Code Playgroud)

然后我有一个base.html和问题/ latest.html

{% extends 'base.html' %}
<img class="hl" src="{{ MEDIA_URL }}/images/avatar.jpg" /></a>
Run Code Online (Sandbox Code Playgroud)

但MEDIA_URL显示空白,我认为这是它的假设,但也许我错了.

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

django django-templates media-url

32
推荐指数
2
解决办法
2万
查看次数

如何从Django模板中获取MEDIA_URL?

我对Django如何使用静态内容进行操作感到困惑.本质上,在settings.py文件中,我们定义MEDIA_URL了在解析静态媒体(如脚本和样式)时要使用的URL的哪些点,以及MEDIA_ROOT对文件系统中的实际位置的引用.

然而,这似乎并不清楚,我怎么能访问MEDIA_URL从一个模板,它是一种-的重要,如果我想使用Django的机制在所有负载静态内容.基本上,我的基本模板看起来有点像这样:

<html>
   <head>
       {% block styles %}
       <link rel="stylesheet" href="{{ MEDIA_URL }}styles/master.css"/>
       {% endblock %}
       <title>{% block title %}Page Title{% endblock %}</title>
   </head>
   <body>
      {% block scripts %}
      <script type="text/javascript" src="{{ MEDIA_URL }}scripts/jquery.js"></script>
      {% endblock %}
   </body>
</html>
Run Code Online (Sandbox Code Playgroud)

以上代码是否真的有效?我听说你必须使用其他插件才能得到类似这样的东西,这看起来很奇怪,因为推测定义背后的重点MEDIA_URL是在模板中使用它.

django django-templates

3
推荐指数
1
解决办法
7099
查看次数

标签 统计

django ×2

django-templates ×2

media-url ×1