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
所以在我的设置。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', )
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
在url。py我有页面的常规模式(模板加载得很好),并且有这个额外的行
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
在模板文件索引中。html我有行
<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
但它从来没有展示过图像。甚至当我试图直接访问图像文件时,也可以直接访问http://127.0.0.1:8000/mattr/public/media/images/Mattr1。它给我一个没有找到错误的页面。我有点搞不清路径从哪里开始,但是因为我的模板页面加载,我认为路径是正确的
3 个解决方案
#1
1
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
不要忘记昏迷或者django的管理员没有css。已经完成了,无需更改url中的任何内容
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
:
把这个放在myproject的底部。网址:
import settings
urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT,}),)
done!
完成了!
#2
0
Using '/' in front of the path denotes you are referring from root directory which I guess is not the case. Try doing this.
在路径前面使用'/'表示您是从根目录引用的,我猜不是这样的。试着这样做。
STATIC_ROOT = 'mattr/static/'
#3
0
Try this in your settings.py:
在你的生活中试试这个。
import os
DIRNAME = os.path.dirname(__file__)
STATIC_ROOT = os.path.join(DIRNAME, 'static')
STATIC_URL = '/static/'
in your templates:
在你的模板:
{{STATIC_URL}}css/etc...
You are also overwriting your TEMPLATE_CONTEXT_PROCESSORS
instead do this: in your settings.py
您也要重写template_context_processor,而不是这样做:在您的设置中。py。
import django.conf.global_settings as DEFAULT_SETTINGS
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
"whatever_your_adding",
)
#1
1
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
不要忘记昏迷或者django的管理员没有css。已经完成了,无需更改url中的任何内容
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
:
把这个放在myproject的底部。网址:
import settings
urlpatterns += patterns('', url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT,}),)
done!
完成了!
#2
0
Using '/' in front of the path denotes you are referring from root directory which I guess is not the case. Try doing this.
在路径前面使用'/'表示您是从根目录引用的,我猜不是这样的。试着这样做。
STATIC_ROOT = 'mattr/static/'
#3
0
Try this in your settings.py:
在你的生活中试试这个。
import os
DIRNAME = os.path.dirname(__file__)
STATIC_ROOT = os.path.join(DIRNAME, 'static')
STATIC_URL = '/static/'
in your templates:
在你的模板:
{{STATIC_URL}}css/etc...
You are also overwriting your TEMPLATE_CONTEXT_PROCESSORS
instead do this: in your settings.py
您也要重写template_context_processor,而不是这样做:在您的设置中。py。
import django.conf.global_settings as DEFAULT_SETTINGS
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
"whatever_your_adding",
)