gpt4 book ai didi

python - 即使使用 --insecure 后 Django 图像也不会加载

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:09 24 4
gpt4 key购买 nike

我尝试制作自定义 404 错误页面。我将 debug=True 更改为 debug=False 并将本地 django 服务器运行为 >python manage.py runserver --insecure。这样做网站正在加载 css 但没有加载图像。即使使用 --insecure 后,所有图像也未加载。当 debug=True 时,所有 css 和图像都会加载。我删除了 settings.py 的一些额外部分,希望下面涵盖重要的部分。

设置.py
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DEBUG = False

ALLOWED_HOSTS = ['*']

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'login',
'upload',
'ckeditor',
'ckeditor_uploader',
'django_bootstrap_carousel'
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'MySite.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'TorrentSite.wsgi.application'

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
DEFAULT_FROM_EMAIL = '-'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
EMAIL_PORT = 1025

MEDIA_ROOT = 'D:/projects/PyCharm projects/TorrentSite/media/'
MEDIA_URL = '/media/'

STATIC_ROOT = 'D:/projects/PyCharm projects/TorrentSite/upload/static/'
STATIC_URL = '/static/'

urls.py
from django.conf.urls import url, include
from django.contrib import admin
from upload import views
from TorrentSite import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

handler404 = 'upload.views.custom_404'

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home_page, name='home_page'),
url(r'^games/', include('upload.urls')),
url(r'^contact/', views.contact_page, name='contact_page'),
url(r'^search/$', views.search_result, name='search-result')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

最佳答案

尝试将其添加到project/urls.py

...
from django.views.static import serve
...

urlpatterns = [
...
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT, }),
...
]

关于python - 即使使用 --insecure 后 Django 图像也不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41465391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com