如何将静态样式文件链接到 Apache 上的 Django 应用程序



我正在使用Django在Ubuntu VM上设置ApacheWeb服务器,目的是将其配置为生产环境。我使用这个演练来启动并运行 Apache 和 Django,从那时起,我一直在遵循 Django 文档中提供的官方教程。我已经进入了第 6 部分,其中讨论了管理静态文件,但我似乎无法应用样式。

缩写服务器文件结构:

/etc/
--apache2/
----apache2.conf
....
/build/
--django/ <-- Django installation
--tstdj/ <-- target project
----manage.py
----polls/
------...
------static/
--------polls
----------styles.css
------templates/
----------....
----------index.html
------urls.py
------views.py
----static/
------....
------polls/
--------styles.css
----tstdj/
------....
------settings.py
------urls.py
------wsgi.py

/etc/apache2/apache2.conf:

....
WSGIDaemonProcess www-data processes=2 threads=12 python-path=/build/tstdj
WSGIProcessGroup www-data
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /build/tstdj/tstdj/wsgi.py
Alias /static/ /build/tstdj/static
<Directory /build/tstdj/tstdj>
Require all granted
</Directory>
<Directory /build/tstdj/static>
Require all granted
</Directory>
<Directory /static>
Require all granted
</Directory>

/build/tstdj/tstdj/settings.py:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
....
STATIC_ROOT = BASE_DIR + '/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
'/build/tstdj/polls/static',
]
#STATIC_DIRS = 'static'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

/build/tstdj/polls/templates/polls/index.html:

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/styles.css' %}">
<p><span>My name Jeff</span></p>
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.        question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}

/build/tstdj/polls/static/polls/styles.css:

li a {
color: green;
}

显然,在这种情况下,所需的输出是具有绿色链接。检查器的"网络"选项卡在 styles.css 文件上显示 403 错误,尝试直接转到localhost:8080/static/也是如此。

我跑过python manage.py collectstaticsudo service apache2 restart天知道多少次。我知道有一些方法可以让样式在开发中工作,但我还没有让它们在生产中发挥作用。

我的问题是我试图在/etc/apache2/apache2.conf而不是/etc/apache2/sites-available/000-default.conf中更改权限。

Apache 403 同时提供 Django 静态文件

最新更新