Django在模板中找不到静态图像 [英] Django can't find static images in templates

查看:118
本文介绍了Django在模板中找不到静态图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看了几乎所有的例子在这里和文档中,它根本不起作用。



所以在我的设置中。 py 文件我有

  STATIC_ROOT ='/ mattr / static /'
STATIC_URL ='/ mattr / public /'

STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',)

TEMPLATE_CONTEXT_PROCESSORS =('django.core.context_processors.static',)

TEMPLATE_DIRS =('mattr / public',)
/ pre>

基本上处理静态文件所需的一切。



urls.py 我有正常的页面模式(模板加载很好),并有这个额外的行

  urlpatterns + = staticfiles_urlpatterns()

views.py 中我有(这是主页):

  def home(request):
t = get_template('index.html');
html = t.render(RequestContext(request))
return HttpResponse(html)

在模板文件 index.html 中,我有行

 < img src ={{STATIC_URL}} media / images / Mattr1.png> 

但它从不显示图像。即使我尝试直接在http://127.0.0.1:8000/mattr/public/media/images/Mattr1.png 直接转到图像文件,它会给我一个未找到页面的错误。
我有点困惑,路径从哪里开始,但是因为我的模板页面加载,我想我有正确的路径

解决方案

p>当你在谈论静态文件时,请执行以下操作:

  STATIC_URL ='/ static /'#or无论你想要什么

STATICFILES_DIRS =(
'/ path / to / static / root / directory /',

不要忘记昏迷或django的管理员将不会有其CSS。
完成后,如果您正在说话,则无需更改 urls.py



中的任何内容关于媒体,请执行以下操作:

  MEDIA_ROOT ='/ media /'#或任何你想要的

MEDIA_URL ='/ path / to / media / root / directory'

并将其放在底部 myproject.urls

 导入设置
urlpatterns +模式('',url(r'^ media /(?P< path>。*)$','django.views.static.serve',{'document_root':settings.MEDIA_ROOT,}))

完成!


I've looked at pretty much all the examples here and in the documentation and it just isn't working at all

So in my settings.py file I have

STATIC_ROOT = '/mattr/static/'
STATIC_URL = '/mattr/public/'

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',)

TEMPLATE_CONTEXT_PROCESSORS = ('django.core.context_processors.static',)

TEMPLATE_DIRS = ('mattr/public', )

Basically everything needed to handle static files.

In urls.py I have the normal patterns for pages (templates load just fine) and have this extra line

urlpatterns += staticfiles_urlpatterns()

In views.py I have (this is for the homepage):

def home(request):
    t = get_template('index.html');
    html = t.render(RequestContext(request))
    return HttpResponse(html)

And in the template file index.html I have the line

<img src="{{ STATIC_URL }}media/images/Mattr1.png">

And yet it never shows images. Even when I try to just go to the image file directly at http://127.0.0.1:8000/mattr/public/media/images/Mattr1.png it gives me a Page Not Found error. I was a bit confused where the path starts from but because my template page loads I figured I had the paths correct

解决方案

when you're talking about static files, do this :

STATIC_URL = '/static/' #or whatever you want

STATICFILES_DIRS = (
    '/path/to/static/root/directory/',
)

Don't forget the coma or django's admin won't have its css. It's done, no need to change anything in the urls.py

if you're talking about media, do this :

MEDIA_ROOT = '/media/' # or whatever you want

MEDIA_URL = '/path/to/media/root/directory'

and place this at the bottom at myproject.urls :

import settings
urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT,}),)

done!

这篇关于Django在模板中找不到静态图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆