无法让Django提供静态文件 [英] Can't get Django to serve static files
问题描述
我知道SO上有很多类似的线程,实际上是同样的问题 - 但是我无法解决我的问题
Django版本
>>> import django
>>>打印django.get_version()
1.6.2
我想访问静态文件(css ,图像,..)从模板。
< link rel =stylesheettype =text / csshref =styles.css/>
我的文件夹结构
项目
| - 项目
| - settings.py
| - ...
| - app
| - views.py
| - ...
| - 模板
| - a_template.html
| - static
| - styles.css
settings.py(重要部分)
BASE_DIR = os.path.dirname(os.path.dirname(__ file__))
INSTALLED_APPS =(
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'server',
)
STATIC_URL ='/ static /'
我为 STATIC_URL
(甚至是绝对路径)尝试了很多不同的解决方案,但没有一个为我工作。
首先,我将使用addi的 static
模板标签ng到模板的顶部:
{%load static%}
/ pre>
然后在href:
href = {%staticstyles.css%}
那么,我会更改我的
urls.py
,并为您的开发服务器提供静态文件的可能性:从django.contrib.staticfiles.urls import staticfiles_urlpatterns
/ *你的url模式在这里* /
如果settings.DEBUG:
urlpatterns + = staticfiles_urlpatterns()
应该做的诀窍...
I know there are a ton of similiar threads on SO with actually the same question - but I can't solve my problem
Django Version
>>> import django >>> print django.get_version() 1.6.2
I want to access static files (css, images, ..) from a template.
<link rel="stylesheet" type="text/css" href="styles.css" />
My folder structure
Project |-- Project |-- settings.py |-- ... |-- app |-- views.py |-- ... |-- templates |-- a_template.html |-- static |-- styles.css
settings.py (important parts)
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'server', ) STATIC_URL = '/static/'
I tried a lot different solutions for
STATIC_URL
(even absolute paths) but none of them worked for me.解决方案first, I would use the
static
template tag by adding to the top of the template:{% load static %}
and then, in the href:
href="{% static "styles.css" %}"
then, I would change my
urls.py
and add the possibility for your development server to serve static files:from django.contrib.staticfiles.urls import staticfiles_urlpatterns /* you url patterns here */ if settings.DEBUG: urlpatterns += staticfiles_urlpatterns()
that should do the trick...
这篇关于无法让Django提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!