Django / Apache / Ubuntu无法提供静态文件

时间:2020-09-23 16:37:27

标签: python django apache wsgi django-staticfiles

一切正常,直到我从本地PC移到Web服务器(AWS Ubuntu)。我正在将Apache与WSGI适配器一起使用。

DEBUG = FALSE,这是一个实时生产环境。

我遵循了所有可能已经做过的所有指南,但仍然无法弄清为什么我的图像或样式都没有显示。

我无法全神贯注于静态文件的工作方式,可能有一个简单的原因为什么它不起作用,但我无法弄清楚。我遵循了很多指南,却一头雾水。

是的,我已经运行collectstatic,但似乎要做的就是将我所有的静态文件都放入静态文件夹中。不确定这样做的目的是什么,我在django网站上无法理解它。

您可以在这里查看我的网站:http://13.56.18.7/

该网站加载时没有样式,如果您检查Chrome开发工具,则不会显示我的文件:

enter image description here

内部版本:

enter image description here

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 %}

1 个答案:

答案 0 :(得分:0)

看到网站和控制台中的错误后,我认为是在更改

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。我无法从提供的信息中得知为什么会这样。您可以使用主页中加载的完整模板来更新问题吗?

相关问题