Django 1.6.1:静态标签不工作 [英] Django 1.6.1: static tag not working
问题描述
我已经花了几个小时,看到无处不在,但无法弄清楚,所以我从更有经验的一次求救。
I have spent hours on this and looked everywhere, but wasn't able to figure out, so I beg help from more experienced once.
这不是我的我第一次创建一个django应用程序,所以我引用了以前做过的,但没有任何帮助。我正在尝试使用静态模板标签来提供静态文件,但它不起作用。
It is not my first time I am creating a django app, so I referenced what I did before, but nothing helps. I am trying to serve static files using "static" template tag, but it doesn't work.
我很确定我正确设置一切(以下教程和我的以前的项目;我将提供下面的设置),但是当我在本地运行服务器时,它找不到我的静态文件。当我用{{STATIC_URL}}标签替换静态模板标签时,它可以正常工作。我甚至在浏览器中输入直接路径到静态文件,所有这些都可以工作。我得到了CSS和JS文件的输出。这是我疯狂的,静态模板标签不起作用。
I am pretty sure I set up everything correctly(following tutorials and my previous projects; i'll provide settings below), but when I run server locally, it can't find my static files. When I replace static template tag with {{STATIC_URL}} tag it works. I even type the direct path in my browser to the static files, all of them work. I get output of CSS and JS files. It is driving me crazy, that the static template tag is not working.
这是我的设置:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = 'staticfiles'
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/w/static/'
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
#ADMIN_MEDIA_PREFIX = '/admin/media/'
# Additional locations of static files
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static/'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',)
Btw,我检查了静态文件夹,它是正确的。我故意d idn't不包括应用程序的名称,所以它在那里。我还包括{%load static from staticfiles%}标签。正如我所说,只有静态标签不能正常工作。请,有人解释我正在做什么愚蠢的错误。
Btw, i checked the path for static folder and it is correct. I deliberately didn't include the name of the app, so it is there. I also include {%load static from staticfiles%} tag. As i mentioned only static tag isn't working. Please, somebody explain what stupid mistake I am making.
谢谢
推荐答案
p>我正在使用django 1.6.1
I'm using django 1.6.1
这是我的设置:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, "templates"),)
我的文件结构如下所示:
My file structure looks like this:
mysite
myapp
static
templates
manage.py
编辑:
它是{ %load staticfiles%}不{%load static from staticfiles%}
It's {% load staticfiles %} not {%load static from staticfiles%}
您不需要使用STATIC_ROOT,除非您尝试在每个应用程序中有一个静态文件夹相信
You don't need to use STATIC_ROOT unless your trying to have a static folder in each app, I believe
我有这个问题
例如.jpg!= .jpeg!所以也要找出这个错误
I had this question For example .jpg != .jpeg! So look for that mistake as well
这篇关于Django 1.6.1:静态标签不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!