目前在用nginx+gunicorn对django进行部署

当我用gunicorn -w 4 -b 127.0.0.1:8080 myproject.wsig:application启动django时访问主页却发现所有static文件夹下的静态文件都找不到,部分如下:

Not Found: /static/js/app.min.js
Not Found: /static/js/custom.js
Not Found: /static/img/user1.jpg
Not Found: /static/plugins/bootstrap/js/bootstrap.min.js
Not Found: /static/plugins/morris/raphael-2.1.0.min.js
Not Found: /static/plugins/jquery-ui-1.11.0.custom/jquery-ui.js
Not Found: /static/js/sb-admin.js
Not Found: /static/js/app.min.js

用python manage.py runserver启动时是没有问题的(可以见上一篇文章django解决静态文件依赖问题以及前端引入方式),

搜了一下午终于找到了解决方法(详见),即在urls中添加以下代码

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# ... the rest of your URLconf goes here ...

urlpatterns += staticfiles_urlpatterns()

---------------------------------------------不知道要分割啥的分割线------------------------------------------
看一下源码,该方法返回一个static类

from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.views import serve urlpatterns = [] def staticfiles_urlpatterns(prefix=None):
"""
Helper function to return a URL pattern for serving static files.
"""
if prefix is None:
prefix = settings.STATIC_URL #这里 static_url = '/static/'
return static(prefix, view=serve) # Only append if urlpatterns are empty
if settings.DEBUG and not urlpatterns:
urlpatterns += staticfiles_urlpatterns()

再看一下这个static

def static(prefix, view=serve, **kwargs):
"""
Helper function to return a URL pattern for serving files in debug mode. from django.conf import settings
from django.conf.urls.static import static urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
"""
# No-op if not in debug mode or an non-local prefix
if not settings.DEBUG or (prefix and '://' in prefix):
return []
elif not prefix:
raise ImproperlyConfigured("Empty static prefix not permitted")
   # Serve static files below a given point in the directory structure.
  return [ url(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs), ]

这里view=server就不细看了,其中心思想即把目录结构中的静态文件挂载到一个给定的路径上,这样前端html中的link或者src属性就能根据这一路径找到目标文件。

DONE.

[Django基础] gunicorn启动django时静态文件的加载的更多相关文章

  1. Django项目关闭debug模式后,静态文件无法加载的解决办法

    开启内置服务器,由于项目中local_settings.py文件中的DEBUG=True,进行开发和调试一直没什么问题. 但是现在需要编写404,500等出错页面,在debug模式下出了错都会出现报错 ...

  2. gunicorn启动django时静态文件的加载

    目前在用nginx+gunicorn对django进行部署 当我用gunicorn -w 4 -b 127.0.0.1:8080 myproject.wsig:application启动django时 ...

  3. django在关闭debug后,admin界面 及静态文件无法加载的解决办法

    当debug为true的时候,ALLOWED_HOSTS是跳过不管用的.所以这里需要将debug关掉,令debug=false,ALLOWED_HOSTS=[ '*' ]表示所有的主机都可以访问 开启 ...

  4. Django---MTV和MVC的了解,Django的模版语言变量和逻辑,常见的模板语言过滤器,自定义过滤器,CSRF了解,Django的母版(继承extends,块block,组件include,静态文件的加载load static),自定义simple_tag和inclusion_tag

    Django---MTV和MVC的了解,Django的模版语言变量和逻辑,常见的模板语言过滤器,自定义过滤器,CSRF了解,Django的母版(继承extends,块block,组件include,静 ...

  5. django 中静态文件项目加载问题

    问题描述: django项目中创建了多个app后,每个app中都有对应的static静态文件.整个项目运行时这些静态文件的加载就是一个问题,因为整个项目我只参与了一部分,项目部署之类的并没有参与.我写 ...

  6. django 项目运行时static静态文件不能加载问题处理

    一.首先检查网页中的加载路径是否正确,如果和文件所在路径不一致,就把html改下路径 二.加载路径和文件实际路径一致,看下配置文件: STATIC_URL = '/static/'STATIC_ROO ...

  7. Web前端性能优化——如何有效提升静态文件的加载速度

    WeTest 导读 此文总结了笔者在Web静态资源方面的一些优化经验. 一.如何优化 用户在访问网页时, 最直观的感受就是页面内容出来的速度,我们要做的优化工作, 也主要是为了这个目标.那么为了提高页 ...

  8. web前端性能优化,提升静态文件的加载速度

    原文地址:传送门 WeTest 导读 此文总结了笔者在Web静态资源方面的一些优化经验. 如何优化 用户在访问网页时, 最直观的感受就是页面内容出来的速度,我们要做的优化工作, 也主要是为了这个目标. ...

  9. IDEA问题之“微服务启动项目时,不会加载Spring Boot到Services中”

    1.启动项目时,不会加载Spring Boot到Services中 现象解析: 启动项目时 会在debug的位置加载项目 注:这里没有配图,因为问题已解决,未记录图,需往后遇到记录 解决方案: 需要在 ...

随机推荐

  1. org-table

    ‎ Table of Contents 1. table 1.1. 创建方式 1.2. 重新对齐 1.3. 行列编辑 1.4. 区域 1.5. 计算 1.6. 其他的 1.7. 行宽度 1.8. 列分 ...

  2. idea之查看类的上下级继承关系

  3. vscode调试单个PHP脚本文件

    1.安装完vscode里的debug插件后, 在WorkSpace setting:添加上php的可执行文件路径: 2.下载适合自己PHP版本的Xdebug 3.在PHP目录下的php.ini配置文件 ...

  4. xmpp 消息和好友上下线(3)

    原始地址:XMPPFrameWork IOS 开发(四) 消息 //收到消息 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XM ...

  5. python socket实现文件传输(防粘包)

    1.文件传输的要点: 采用iterator(迭代器对象)迭代读取,提高读取以及存取效率: 通过for line in file_handles逐行conn.send(): 2.socket粘包问题: ...

  6. Python-文件和数据格式化

    文件的使用 >文件的类型 文件的理解:文件是数据的抽象和集合 -文件时存储在辅助存储器上的数据序列 -文件是数据存储的一种形式 -文件展现形态:文本文件和二进制文件 文本文件vs.二进制文件 - ...

  7. 2.js原型的基本概念

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. BZOJ 3894 Luogu P4313 文理分科 (最小割)

    题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3894 (luogu) https://www.luogu.org/pro ...

  9. 数论结论 nefu 702

    Given a prime p (p<108),you are to find min{x2+y2},where x and y belongs to positive integer, so ...

  10. 临时起异,要进入C++领域耍一个程序

    没办法.两周之内可以搞定吧. 就一个SESSION 0的问题. 网上有类似源码,调一下应该就可以吧..保佑顺利. 基本语法都还记得,快N年啦... #include <iostream> ...