Django 1.3 静态文件并使用自定义视图显示内容
使用自定义视图显示图像时出现一个小问题。 我已根据需要使用了请求上下文,但是图像未显示。
我的模型使用缩略图将图像创建到名为“timgs”的文件夹中,该文件
夹位于 static/myapp/timgs 下。静态文件目录可以很好地显示 css/images,但是当涉及到自定义视图时,图像不会显示。
我尝试过在这样的设置中列出特定文件夹。没有运气。
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static/myapp/'),
("timgs", "/timgs"),
)
如果有人愿意解释出什么问题,或者提供一个简单的例子,我将非常感激。 谢谢。
视图/模板
def function(request):
whatever = ____.objects.all()
return render_to_response('template', {
'whatever': whatever,
}, context_instance=RequestContext(request))
{% for whatever in whatever %}
<li>{{ whatever.text }}</li>
<li><img src="{{ whatever.image.thumbnail }}" /></li>
{% endfor %}
设置
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
A slight problem displaying an image using custom views.
I have used the request context as required, however the image doesn't show.
My model uses thumbnails creating the image into a folder named "timgs" which lives under static/myapp/timgs
The static files directory displays css/images fine however when it comes to a custom view images do not display.
Ive tried listing the specific folder in settings like this. with no luck.
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static/myapp/'),
("timgs", "/timgs"),
)
I would be very grateful if someone would be kind enough to explain what's wrong, maybe provide a simple example.
Thank you.
view/template
def function(request):
whatever = ____.objects.all()
return render_to_response('template', {
'whatever': whatever,
}, context_instance=RequestContext(request))
{% for whatever in whatever %}
<li>{{ whatever.text }}</li>
<li><img src="{{ whatever.image.thumbnail }}" /></li>
{% endfor %}
settings
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须列出每个静态目录的绝对路径
You must list the absolute paths for every static dir