Django / Apache / Ubuntu 未提供静态文件
Django / Apache / Ubuntu Static files not being served
在我从本地 PC 转移到 Web 服务器 (AWS Ubuntu) 之前,一切正常。我正在使用带有 WSGI 适配器的 Apache。
DEBUG = FALSE
所以这是一个现场生产环境。
我遵循了所有可能的指南,并且已经这样做了好几天,但仍然无法弄清楚为什么 none 我的图像或样式会显示。
我无法理解静态文件是如何工作的,这可能有一个简单的原因导致它不起作用,但我无法弄清楚。看了那么多攻略,不知所措
是的,我有 运行 collectstatic
,但这似乎只是将我所有的静态文件都扔到静态文件夹中。不确定这样做的目的是什么,我在 django 网站上无法理解它。
您可以在这里查看我的网站:http://13.56.18.7/
网站加载时没有样式,如果您检查 Chrome 开发工具,它会显示找到 none 个我的文件:
建造:
urls.py
from django.contrib import admin
from django.urls import path
from app_main.views import (home_page, services_page, history_page, offers_page, contact_page)
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', home_page, name='home_page'),
path('admin/', admin.site.urls),
path('services/', services_page),
url(r'^services/', services_page, name='services_page'),
url(r'^history/', history_page, name='history_page'),
url(r'^offers/', offers_page, name='offers_page'),
url(r'^contact/', contact_page, name='contact_page'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_DIR = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
if DEBUG:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
apache2.conf
WSGIDaemonProcess www-data processes=2 threads=12 python-path=/build/DoneRitePlumbing
WSGIProcessGroup www-data
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /build/DoneRitePlumbing/DoneRitePlumbing/wsgi.py
<Directory /build/DoneRitePlumbing/DoneRitePlumbing>
Require all granted
</Directory>
Alias /media/ /build/DoneRitePlumbing/media/
Alias /static/ /build/DoneRitePlumbing/static/
<Directory /build/DoneRitePlumbing/static>
Require all granted
</Directory>
<Directory /build/DoneRitePlumbing/media>
Require all granted
</Directory>
在我的脑袋里 base.html:
{% load static %}
<link rel='stylesheet' type="text/css" href="{% static 'css/styles.css' %}">
Home.html
{% extends "base.html" %}
{% load static %}
{% block contentHome %}
<style>
.parallax {
/* The image used */
background-image: url("{% static 'images/FrontMain-2.png' %}");
/* Set a specific height */
height: 300px;
}
</style>
base.html
{% load static %}
<!doctype html>
<html lang="en">
<head>
<link rel='stylesheet' type="text/css" href="{% static 'styles.css' %}">
</head>
<body>
{% include 'navbar.html' %}
{% block contentContact %}
{% endblock %}
{% block contentServices %}
{% endblock %}
{% block contentHome %}
{% endblock %}
{% block contentOffers %}
{% endblock %}
{% block contentHistory %}
{% endblock %}
在看到您的网站和控制台中的错误后,我想,改变
STATIC_URL="/staic/"
至
STATIC_URL="http://13.56.18.7/static/"
in settings.py 在你的作品中应该可以解决你的问题。
您在模板中使用的 {% static 'css/styles.css' %}
应将 href
设置为 /static/css/styles.css
,而是将其设置为 /build/DoneRitePlumbing/static/css/styles.css
。我无法从提供的信息中判断为什么会发生这种情况。您可以使用主页中加载的完整模板更新问题吗?
在我从本地 PC 转移到 Web 服务器 (AWS Ubuntu) 之前,一切正常。我正在使用带有 WSGI 适配器的 Apache。
DEBUG = FALSE
所以这是一个现场生产环境。
我遵循了所有可能的指南,并且已经这样做了好几天,但仍然无法弄清楚为什么 none 我的图像或样式会显示。
我无法理解静态文件是如何工作的,这可能有一个简单的原因导致它不起作用,但我无法弄清楚。看了那么多攻略,不知所措
是的,我有 运行 collectstatic
,但这似乎只是将我所有的静态文件都扔到静态文件夹中。不确定这样做的目的是什么,我在 django 网站上无法理解它。
您可以在这里查看我的网站:http://13.56.18.7/
网站加载时没有样式,如果您检查 Chrome 开发工具,它会显示找到 none 个我的文件:
建造:
urls.py
from django.contrib import admin
from django.urls import path
from app_main.views import (home_page, services_page, history_page, offers_page, contact_page)
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', home_page, name='home_page'),
path('admin/', admin.site.urls),
path('services/', services_page),
url(r'^services/', services_page, name='services_page'),
url(r'^history/', history_page, name='history_page'),
url(r'^offers/', offers_page, name='offers_page'),
url(r'^contact/', contact_page, name='contact_page'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
settings.py
STATIC_DIR = os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
if DEBUG:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
apache2.conf
WSGIDaemonProcess www-data processes=2 threads=12 python-path=/build/DoneRitePlumbing
WSGIProcessGroup www-data
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /build/DoneRitePlumbing/DoneRitePlumbing/wsgi.py
<Directory /build/DoneRitePlumbing/DoneRitePlumbing>
Require all granted
</Directory>
Alias /media/ /build/DoneRitePlumbing/media/
Alias /static/ /build/DoneRitePlumbing/static/
<Directory /build/DoneRitePlumbing/static>
Require all granted
</Directory>
<Directory /build/DoneRitePlumbing/media>
Require all granted
</Directory>
在我的脑袋里 base.html:
{% load static %}
<link rel='stylesheet' type="text/css" href="{% static 'css/styles.css' %}">
Home.html
{% extends "base.html" %}
{% load static %}
{% block contentHome %}
<style>
.parallax {
/* The image used */
background-image: url("{% static 'images/FrontMain-2.png' %}");
/* Set a specific height */
height: 300px;
}
</style>
base.html
{% load static %}
<!doctype html>
<html lang="en">
<head>
<link rel='stylesheet' type="text/css" href="{% static 'styles.css' %}">
</head>
<body>
{% include 'navbar.html' %}
{% block contentContact %}
{% endblock %}
{% block contentServices %}
{% endblock %}
{% block contentHome %}
{% endblock %}
{% block contentOffers %}
{% endblock %}
{% block contentHistory %}
{% endblock %}
在看到您的网站和控制台中的错误后,我想,改变
STATIC_URL="/staic/"
至
STATIC_URL="http://13.56.18.7/static/"
in settings.py 在你的作品中应该可以解决你的问题。
您在模板中使用的 {% static 'css/styles.css' %}
应将 href
设置为 /static/css/styles.css
,而是将其设置为 /build/DoneRitePlumbing/static/css/styles.css
。我无法从提供的信息中判断为什么会发生这种情况。您可以使用主页中加载的完整模板更新问题吗?