出于某种原因,django没有提供我的静态文件.
我已经看过这个问题的一堆修复,但我还没有找到解决方案.
这是我的配置:
urls.py
urlpatterns = patterns('',
(r'^$', index),
(r'^ajax/$', ajax),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': path.join(path.dirname(__file__), 'static')}),
)
Run Code Online (Sandbox Code Playgroud)
settings.py
STATIC_ROOT = '/home/aurora/Code/django/test/static/'
STATIC_URL = '/static/'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
Run Code Online (Sandbox Code Playgroud)
当我导航到http://localhost:8000/static/css/default.css
我时出现此错误:'css/default.css' could not be found
当我导航到http://localhost:8000/static/
我时出现此错误:Directory indexes are not allowed here.
看起来静态目录已被映射出来,但子目录却没有.
Muh*_*man 11
开发中:
STATICFILES_DIRS应该包含所有静态目录,其中所有静态文件都驻留在其中
如果你的文件在本地机器上,STATIC_URL应为"/ static /",否则将基本URL放在这里,例如" http://example.com/ "
INSTALLED_APPS应该包含'django.contrib.staticfiles'
在模板中,加载staticfiles模块:
{% load staticfiles %}
..
..
<img src='{% static "images/test.png" %}' alt='img' />
Run Code Online (Sandbox Code Playgroud)
在生产中:
添加django使用的"STATIC_ROOT"将所有静态文件从"STATICFILES_DIRS"收集到它
收集静态文件
$ python manage.py collectstatic
from . import settings
..
..
urlpatterns = patterns('',
..
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.STATIC_ROOT)}),)
更详细的文章如下:
http://blog.xjtian.com/post/52685286308/serving-static-files-in-django-more-complicated
http://agiliq.com/blog/2013/03/serving-static-files-in-django/
我不认为你需要你的urls.py中的静态路径,删除它,它应该工作.
目前是这样的
urlpatterns = patterns('',
(r'^$', index),
(r'^ajax/$', ajax),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': path.join(path.dirname(__file__), 'static')}),
)
Run Code Online (Sandbox Code Playgroud)
只需删除r'^静态行
urlpatterns = patterns('',
(r'^$', index),
(r'^ajax/$', ajax),
)
Run Code Online (Sandbox Code Playgroud)
至少这是在django 1.3及以上的方式
尝试运行python manage.py collectstatic并查看静态文件的收集位置.
将此添加到您的urls.py设置DEBUG=True中settings.py
if settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes':True}),
)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29125 次 |
| 最近记录: |