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

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

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
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属性就能根据这一路径找到目标文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是一个基本的 Nginx、GunicornDjango 和 DRF 示例,涵盖了常见的配置和设置: 1. 安装和配置 Nginx 在服务器上安装 Nginx,并创建一个新的站点配置文件。以下是一个示例配置文件,将所有请求转发到 Gunicorn: ``` server { listen 80; server_name yourdomain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } ``` 2. 安装和配置 GunicornDjango 项目的虚拟环境中安装 Gunicorn: ``` pip install gunicorn ``` 创建一个 Gunicorn 配置文件(比如 gunicorn_config.py),指定项目的 WSGI 应用程序和其他选项: ``` import multiprocessing bind = "127.0.0.1:8000" workers = multiprocessing.cpu_count() * 2 + 1 ``` 3. 安装和配置 Django 和 DRF 在 Django 项目的虚拟环境中安装 Django 和 DRF: ``` pip install django djangorestframework ``` 创建一个 Django 项目,并在 settings.py 中添加 DRF 的应用程序: ``` INSTALLED_APPS = [ ... 'rest_framework', ... ] ``` 在 urls.py 中添加 DRF 的路由: ``` from django.urls import path, include urlpatterns = [ ... path('api/', include('rest_framework.urls')), ... ] ``` 创建一个 DRF 视图(比如 views.py),实现一个简单的 API: ``` from rest_framework.views import APIView from rest_framework.response import Response class HelloWorldView(APIView): def get(self, request): return Response("Hello, World!") ``` 在 urls.py 中添加一个路由,将视图映射到 URL: ``` from django.urls import path from .views import HelloWorldView urlpatterns = [ ... path('api/hello/', HelloWorldView.as_view()), ... ] ``` 4. 启动应用程序 使用 Gunicorn 启动 Django 项目: ``` gunicorn myproject.wsgi:application -c gunicorn_config.py ``` 访问 http://yourdomain.com/api/hello/,应该会看到 "Hello, World!" 的响应。如果您遇到任何问题,请参阅 Nginx、GunicornDjango 和 DRF 的文档和教程,以获取更详细的说明和指导。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值