找不到静态文件

问题描述 投票:0回答:1
我在向生产服务器提供静态文件时遇到问题。在我的域中加载页面时,我不断收到“警告:django.request:找不到:/static/admin/css/base.css”以及终端中的其他静态文件。

这是我的settings.py 中的一些代码:

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'django_extensions', 'sslserver', 'allauth', 'allauth.account', 'allauth.socialaccount', 'axes', 'rest_framework', ] ... STATIC_URL = 'static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
这是我的 Apache 文件中的 httpd.conf 和 httpd-ssl.conf 中的代码。

Alias /static/ "C:/Users/MyUser/Desktop/MyProject/static/" <Directory "C:/Users/MyUser/Desktop/MyProject/static"> Require all granted </Directory>
在我的 httpd-ssl.conf 中:

Alias /static/ "C:/Users/MyUser/Desktop/MyProject/static/" <Directory "C:/Users/MyUser/Desktop/MyProject/static"> Require all granted </Directory> <Directory "C:/Users/MyUser/Desktop/MyProject/"> Options Indexes FollowSymLinks Require all granted </Directory>
我有我的

DEBUG = False


我还在 Windows VM 上使用 Apache 和 Waitress。我通过以下方式运行我的服务器: 女服务员服务 --listen=127.0.0.1:8000 MyProject.wsgi:application

这是我的项目目录:

MyProject/ ├── manage.py ├── db.sqlite3 ├── static/ │ └── ... (my static files) ├── staticfiles/ │ └── ... (collected static files) └── MyProject2/ ├── settings.py ├── urls.py ├── wsgi.py └── __init__.py
还有我的 wsgi.py:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'HaltScanner.settings') if settings.DEBUG: application = StaticFilesHandler(get_wsgi_application()) else: application = get_wsgi_application() # Add the HaltProject directory to PYTHONPATH sys.path.append(r'C:\Users\MyUser\Desktop\MyProject') # Debugging prints print("PYTHONPATH:", sys.path) print("sys.path:", sys.path) print("os.environ:", os.environ) print("Current Working Directory:", os.getcwd()) # Debugging print for settings print(settings)
除此之外我没有尝试太多,因为我不知道还能做什么。

django apache django-staticfiles static-files
1个回答
0
投票
既然你有

DEBUG = False

,Django将不会处理静态文件。
您可以尝试以下几种解决方案。

MyProject/MyProject2/urls.py

 文件中,您可以添加另一个 urlpattern 来查找 url /static/*:
下的静态文件

from django.views.static import serve from django.conf import settings from django.conf.urls.static import static urlpatterns = [ ..., url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}) ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
如果您遇到任何类型的 mimetype 错误,就像我过去遇到过的那样,您可以在 

MyProject/MyProject2/settings.py

 文件中导入 mimetypes:

import mimetypes mimetypes.add_type("text/css", ".css", True)
此外,如果您仍然想设置

DEBUG = False

并在本地提供静态文件以进行测试,您可以在不安全模式下运行服务器:

python3 manage.py runserver --insecure
    
© www.soinside.com 2019 - 2024. All rights reserved.