django 引入静态文件问题

来源:互联网 发布:ubuntu显示乱码 编辑:程序博客网 时间:2024/05/16 15:25

项目目录:

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 settingsfrom django.conf.urls.static import static


并在结尾追加

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

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

from django.conf.urls import urlfrom django.contrib import adminform blogs import views as blogs_viewsfrom django.conf import settingsfrom django.conf.urls.static import staticurlpatterns = [    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>



0 0
原创粉丝点击