django 引入静态文件问题

 

方法1:

 

 

nlp_server
├── db.sqlite3
├── manage.py
├── nlp_server
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── nlp
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   └── qg
│   │       ├── index.py
│   │       ├── index.pyc
│   │       ├── __init__.py
│   │       ├── __init__.pyc
│   │       ├── QgService.py
│   │       ├── QgService.pyc
│   │       ├── stop.txt
│   │       ├── test.py
│   │       └── test.txt
│   ├── settings.py
│   ├── settings.pyc
│   ├── static
│   │   └── js
│   │       └── jquery.js
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── templates
    └── nlp
        └── qg
            └── index.html

 

static下存放静态文件,templates下存放网页模板文件

 

2.修改setting.py

找到  STATIC_URL = '/static/'  ,把 "/static/"  改为 "static/"  并在后面追加一行,然后保存

1
STATIC_ROOT = os.path. join (BASE_DIR,  'static' )

 最后保存好的样子是这样的:

 

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
 
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

 

 

3.修改 urls.py

 

 

在urls.py中导入2个库

 

from django.conf import settings
from django.conf.urls.static import static

 

 

并在结尾追加

 

 

 

+ static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)

 

最后保存好是这个样子的(红色部分为修改的):

 

from django.conf.urls import url
from django.contrib import adminform blogs import views as blogs_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    url(r'^admin/', admin.site.urls),   url(r'^$', blogs_views.index),
] + static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)

 

4.重新运行你的项目

切记静态文件全都放在 static下面,网页模板文件全都放在 templates下面

最后网页里引用

 

<script type="text/javascript" src="/static/js/jquery.js"></script>

 

 

方法2

 

 

在url.py的底部加入以下代码

1
2
3
from  django.contrib.staticfiles.urls  import  staticfiles_urlpatterns
 
urlpatterns  + =  staticfiles_urlpatterns()


settings.py 静态文件相关示例代码及说明:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')#static 目录的绝对位置

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

life1024

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值