【问题标题】:Django / Apache / Ubuntu Static files not being servedDjango / Apache / Ubuntu 未提供静态文件
【发布时间】: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


    【解决方案1】:

    在看到你的网站和控制台中的错误后,我想,改变

    STATIC_URL="/staic/"
    

    STATIC_URL="http://13.56.18.7/static/"
    

    在您的生产中的 settings.py 应该可以解决您的问题。 您在模板中使用的 {% static 'css/styles.css' %} 应将 href 设置为 /static/css/styles.css ,但将其设置为 /build/DoneRitePlumbing/static/css/styles.css 。我无法从提供的信息中说出为什么会发生这种情况。你能用主页中加载的完整模板更新问题吗?

    【讨论】:

    • 你是个传奇!那行得通。但我仍然遇到另一个问题。我的主页仍未加载某些图像/样式。页面的其余部分看起来不错,除了 2 张图片 + 顶部没有样式。我用我的 home.html 和 base.html 更新了这个问题,如果你能帮我解决这个问题,我会将你的标记为答案。非常感谢您的帮助。
    • 获取 chrome 开发工具显示“13.56.18.7/static/images/FrontMain-2.png”仍未找到 (404)
    猜你喜欢
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 2018-07-21
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    相关资源
    最近更新 更多