Django中没有加载静态文件 [英] Static files are not loaded in Django

查看:68
本文介绍了Django中没有加载静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照这些说明来启用静态我的Django项目中的文件。
我有

  STATIC_URL ='/ static /'
pre>

在我的settings.py文件中。我有'django.contrib.staticfiles',添加到 INSTALLED_APPS =(...) part
我创建了一个文件夹 mysite / static / mysite 并将我的文件放在那里。但是,在我运行服务器之后,我无法访问我的文件,位于 http://127.0.0.1:8000/static/mysite/style.css
我做错了什么?

解决方案

settings.py 包括以下内容:

  import os 
settings_dir = os.path.dirname(__ file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))

STATICFILES_DIRS =(
os.path.join(PROJECT_ROOT,'static / mysite /'),

在您的 urls.py 在末尾包括这些行:

  from django.conf.urls.static import静态
from django.contrib。 staticfiles.urls import staticfiles_urlpatterns
urlpatterns + = staticfiles_urlpatterns()

希望它有帮助。 p>

I'm trying to follow these instructions to enable static files in my Django project. I have

STATIC_URL = '/static/'

in my settings.py file. I have 'django.contrib.staticfiles', added to the INSTALLED_APPS = (...) part I created a folder mysite/static/mysite and put my files there. But after I run the server, I cannot access my files at http://127.0.0.1:8000/static/mysite/style.css. What I have done wrong?

解决方案

In settings.py include this:

import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static/mysite/'),
)

And in your urls.py include these lines at the end:

from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

Hope it helps.

这篇关于Django中没有加载静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆