在AWS Django应用程序上提供静态文件 [英] Serving Static files on AWS Django application

查看:140
本文介绍了在AWS Django应用程序上提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这不是第一次问这个问题,因此我在前面表示歉意.我已经为此工作了几天,但仍然不知道如何进行.

I know that this isn't the first time the question has been asked, so I'm apologizing up front. I've been working on this for a few days and still have no clue how to proceed.

我按照本教程进行了以下操作: https://aws.amazon.com/getting-started/hands-on/deploy-python-application/

I followed this tutorial to the tee: https://aws.amazon.com/getting-started/hands-on/deploy-python-application/

该网站已启动并正在运行,但是当然不会加载静态文件.

The website is up and running, but of course the static files won't load.

我在这里.在settings.py中,我已将STATIC_ROOT和STATIC_URL设置为以下值:

Here's where I'm at. In settings.py, I've set my STATIC_ROOT and STATIC_URL to the following:

STATIC_ROOT = os.path.join(BASE_DIR, 'mysite', 'static')
STATIC_URL = '/static/'

我运行了collectstatic并将所有静态文件收集到mysite应用程序目录中.看起来像这样:

I ran collectstatic and gathered all my static files into the mysite app directory. It looks like this:

-mysite
 - mysite (app)
   - static
     - base
       - base.css
   - settings.py
   - urls.py
   - wsgi.py

不幸的是,静态文件仍然无法在网站上加载.

Unfortunately, the static files still fail to load on the website.

这是我的问题:

  1. 假设我想在网络上查看base.css文本文件.我会去www.mysite.com/static/base/base.css吗?如果不是,URL是什么?如果是,为什么在当前设置下不显示?

  1. Suppose I wanted to view the base.css text file on the web. Would I go to www.mysite.com/static/base/base.css? If no, what would the URL be? If yes, why is it not appearing given the current set up?

根据AWS教程,我编辑了httpd-app.conf文件以包含以下内容

Per the AWS tutorial, I ran edited the httpd-app.conf file to include the following

别名/mysite/static/opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.2.9-py3.7.egg/django/contrib/admin/static

编辑的目的是什么?对静态文件在网站上的投放方式有何影响?

What was the purpose of the edit? How does it impact how the static files are served on the site?

任何帮助您加载这些静态文件的人都将成为救命稻草.

Any help you guys can offer on loading these static files will be a lifesaver.

推荐答案

您的 urls.py 文件需要配置为服务您的 STATIC_URL

Your urls.py file needs to be configured to serve your STATIC_URL

urls.py

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

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

有关提供静态服务的更多信息Django的文件

这篇关于在AWS Django应用程序上提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆