如何解决只有一些静态文件未显示在Django / Apache堆栈上
试图弄清楚世界上所有图像的加载情况如何,除了其中一个模板上的图像很少。请记住,所有这些都可以在开发模式下完美地工作,但是当我将其移至Apache服务器时,就是这样。
这是您可以亲自浏览的我的网站:
我的文件结构:
如果您在首页(/目录)上查找,则会丢失主图像以及位于其正下方的其他2张图像。也缺少许多样式。但是,此下方的所有图像都可以正常显示。
这是有问题的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;
/* Create the parallax scrolling effect */
background-repeat: no-repeat;
background-size: cover;
}
</style>
<!-- Container element -->
<div class="parallax" style="position: relative;">
<div alt="buttonsDiv" style="right: 35px; bottom: 55px; position: absolute;">
<a href="tel:+1-714-605-2950"><button style="margin-right: 20px;" type="button" class="btn btn-primary btn-lg"><i class="fa fa-phone fa-lg"></i> 714-605-2950</button></a>
<a href="{% url 'services_page' %}"> <button type="button" class="btn btn-primary btn-lg"><i class="fa fa-wpforms fa-lg"></i> Contact Form</button></a>
</div>
</div>
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
urlpatterns = [
path('',home_page),path('admin/',admin.site.urls),path('services/',services_page),url(r'^services/',name='services_page'),url(r'^history/',name='history_page'),url(r'^offers/',name='offers_page'),url(r'^contact/',contact_page,name='contact_page'),]
settings.py:
# Static files (CSS,JavaScript,Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_DIR=os.path.join(BASE_DIR,"static")
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,STATIC_DIR+"/css",STATIC_DIR+"/images",]
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
这是我的Apache配置:
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>
WSGIScriptAlias / /build/DoneRitePlumbing/wsgi.py
<Directory /build/DoneRitePlumbing/DoneRitePlumbing>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
“服务”页面上也存在相同的html,并且呈现得很好:
如果您使用chrome开发工具,可以看到它很好地加载了服务映像:
但不是主页:
一定是我很想念的东西,但是我似乎无法弄清楚。
解决方法
这是我的适用于apache的配置
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR,"static")
]
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
urls.py
from django.conf.urls.static import static
urlpatterns = [
...your routes
] + static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
apache #我使用的是Windows,而不是linux
<IfModule wsgi_module>
WSGIPythonPath C:\project\src
WSGIPythonHome C:\project\env
Alias /media C:\project\src\media
Alias /static C:\project\src\static
<Directory C:\project\src\static>
Require all granted
</Directory>
<Directory C:\project\src\media>
Require all granted
</Directory>
WSGIScriptAlias / C:\project\src\settings\wsgi.py
<Directory C:\project\src>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。