Static files in Django 1.6

末鹿安然 提交于 2019-12-10 16:19:21

问题


I'm probably doing multiple things wrong here because I still can't get static files to work correctly in my development environment despite closely following the tutorial. I have a feeling it's because it works slightly differently in Django 1.6, and I can only find answers for previous versions.

Here's my directory structure:

mysite
├───app1
├───mysite
│   └───templates
├───resources
├───static
│   ├───css
│   ├───fonts
│   └───js
└───app2

My installed apps, to prove I have staticfiles on:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app1',
    'app2',
)

My template and static file settings:

 # Templates
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, "mysite/templates"),
)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

I even did this in my urls.py as suggested:

urlpatterns = patterns('',
    ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

And finally, my request:

{% load staticfiles %}
<link href="{% static "css/core.css" %}" rel="stylesheet">

If I navigate directly to http://127.0.0.1/static/css/core.css, I get 'css\core.css' could not be found

Please tell me what I did wrong =[


回答1:


Since the static directory does not "live" in one of the apps (app1, app2 in your case), django can't find the static directory. So with your current structure you need to add the static directory to the STATICFILES_DIRS.

From the documentation:

"Your project will probably also have static assets that aren’t tied to a particular app. In addition to using a static/ directory inside your apps, you can define a list of directories (STATICFILES_DIRS) in your settings file where Django will also look for static files."

Something like this:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATICFILES_DIRS

Hope this helps.



来源:https://stackoverflow.com/questions/19891454/static-files-in-django-1-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!