目前在用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.

gunicorn启动django时静态文件的加载的更多相关文章

  1. [Django基础] gunicorn启动django时静态文件的加载

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. mmap和MappedByteBuffer

    1.MappedByteBuffer是DirectByteBuffer的子类 2.MappedByteBuffer使用的是mmap技术.MappedByteBuffer将文件映射为内存,也可能会被存储 ...

  2. luogu P2659 美丽的序列

    题目背景 GD是一个热衷于寻求美好事物的人,一天他拿到了一个美丽的序列. 题目描述 为了研究这个序列的美丽程度,GD定义了一个序列的“美丽度”和“美丽系数”:对于这个序列的任意一个区间[l,r],这个 ...

  3. argument to nsmutablearray method addobject cannot be nil 警告

    You cannot add nil to an NSMutableArray, and you will raise an exception if you try to. There's NSNu ...

  4. js获取table的值,js获取td里input的值

    1.如果想让table具有可以编辑的功能,可以在table里嵌入input标签 写法{{ list_one[1] or '' }}的作用是,当list_one[1]取值为None时,前端web界面不至 ...

  5. xgboost原理及并行实现

    XGBoost训练: It is not easy to train all the trees at once. Instead, we use an additive strategy: fix ...

  6. Objective-C面向对象-对象和类

    文章都是先由本人个人博客:孙占兴:www.teilim.com,先更新,随后CSDN博客才会更新,掌握第一动态请关注本人主站. 原文链接:http://www.teilim.com/objective ...

  7. 简单的dp hdu 数塔(水题)

    数塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  8. 浅谈JavaScript的函数表达式(递归)

    递归函数,在前面的博客中已经简单的介绍了.递归函数是一个通过函数名称在函数内部调用自身的函数.如下: function fac(num){ if(num<1){ return 1; } else ...

  9. JAVA学习第六十五课 — 正則表達式

    正則表達式:主要应用于操作字符串.通过一些特定的符号来体现 举例: QQ号的校验 6~9位.0不得开头.必须是数字 String类中有matches方法 matches(String regex) 告 ...

  10. Python 003- 小知识汇总(更新中)

    #查询key是否存在,可以在使用未知的字典的时候使用 #-*- coding:utf-8 -*- D={'a':1,'c':3,'b':2} for key in sorted(D): print(k ...