gpt4 book ai didi

Django:在debug = false上提供静态文件

转载 作者:行者123 更新时间:2023-12-02 11:23:06 24 4
gpt4 key购买 nike

我知道这个问题被问过很多次,但是我找到并尝试过的答案都没有帮助我。

这些是我的静态文件设置:

STATIC_ROOT = os.path.abspath(SETTINGS_PATH+'/staticfiles/')

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/staticfiles/'

# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(os.path.dirname(__file__), 'static'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

并在myapp/urls.py中:
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

admin.autodiscover()

urlpatterns = patterns('',
# urls
)

urlpatterns += staticfiles_urlpatterns()

Collectstatic将所有文件复制到staticfiles/,并且我在所有静态文件上都得到404。

我也在urls.py中尝试过此操作:
if not settings.DEBUG:
urlpatterns += patterns('',
url(r'^staticfiles/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}),
)

这给了我以下几种错误:
Resource interpreted as Stylesheet but transferred with MIME type text/html:  "http://localhost:8000/?next=/staticfiles/css/bootstrap.css". localhost:11
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/css/style.css". localhost:12
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/js/bootstrap.js". localhost:79
Resource interpreted as Script but transferred with MIME type text/html: "http://localhost:8000/?next=/staticfiles/js/login.js".

我看不到我的设置有什么问题。任何想法都是最欢迎的。

最佳答案

在settings.py::中

if DEBUG: 
STATIC_ROOT = os.path.join(BASE_DIR, '/static')
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
并在urls.py中添加
import django.views.static
urlpatterns += [
url(r'^static/(?P<path>.*)$', django.views.static.serve, {'document_root': settings.STATIC_ROOT, 'show_indexes': settings.DEBUG})
]

关于Django:在debug = false上提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13801969/

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