我是Django的新手,我正在尝试通过一个名为'dubliners'的简单项目和一个名为'book'的应用程序来学习它.目录结构如下:
dubliners/book/ [includes models.py, views.py, etc.]
dubliners/templates/book/
Run Code Online (Sandbox Code Playgroud)
我有一个JPG文件需要显示在每个网页的标题中.我应该在哪里存储文件?我应该使用哪条路径让标签使用模板显示它?我尝试过各种各样的地点和路径,但到目前为止还没有任何工作.
...
感谢下面的答案.但是,我已经尝试了图像的相对和绝对路径,并且我仍然在网页中显示一个损坏的图像图标.例如,如果我的主目录中有图像并在我的模板中使用此标记:
<img src="/home/tony/london.jpg" />
Run Code Online (Sandbox Code Playgroud)
图像不显示.但是,如果我将网页保存为静态HTML文件,则会显示图像,因此路径正确.也许Django附带的默认Web服务器只有在特定路径上才会显示图像?
在过去的几个小时里,我一直在争论这个问题.我无法显示{{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显示空白,我认为这是它的假设,但也许我错了.
更新 最新版本修复了这些问题.