【发布时间】:2020-09-23 16:37:27
【问题描述】:
在我从本地 PC 转移到 Web 服务器 (AWS Ubuntu) 之前,一切正常。我正在使用 Apache 和 WSGI 适配器。
DEBUG = FALSE 所以这是一个实时生产环境。
我遵循了所有可能的指南,并且已经在这方面工作了几天,但仍然无法弄清楚为什么我的图像或样式都没有显示。
我无法理解静态文件是如何工作的,这可能有一个简单的原因导致它无法正常工作,但我无法弄清楚。我跟着这么多指南,不知所措。
是的,我已经运行了collectstatic,但这似乎只是将我所有的静态文件放入静态文件夹中。不知道这样做的目的是什么,我无法在 django 网站上理解它。
您可以在这里查看我的网站:http://13.56.18.7/
网站加载时没有样式,如果你检查 Chrome 开发工具,它显示没有找到我的文件:
构建:
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' %}">
主页.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 %}
【问题讨论】:
标签: python django apache wsgi django-staticfiles