簡體   English   中英

Django升級:不提供靜態文件

[英]Django Upgrade: Static Files Not Served

我已經從1.9.6升級到Django 1.10。 以下是我以前使用過的urls.py文件:

from django.conf.urls import include, url
from django.contrib import admin
from django.views.static import serve
from dwad import settings

urlpatterns = [
    url(r'', include('meta.urls')),
    url(r'^straightred/', include('straightred.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^chaining/', include('smart_selects.urls')),
    url(r'^tinymce/', include('tinymce.urls')),
    url(r'^accounts/', include('allauth.urls')),
]

# Get Django to serve media files in debug mode.
if settings.DEBUG:
    urlpatterns += [url(r'^resources/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})]

if not settings.DEBUG:
    urlpatterns += [
        url(r'^resources/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
            {'document_root': settings.STATIC_ROOT}),
    ]

當嘗試運行該網站時,我得到:

view must be a callable or a list/tuple in the case of include().

我知道上面的錯誤是由於字符串中的'django.views.static.serve'。 如果我從字符串中刪除它們並鏈接到實際視圖,則會出現以下錯誤:

name 'django' is not defined

如果我從“使Django以調試模式提供媒體文件”中刪除所有內容, 並且網站加載以下,但不提供任何靜態文件或媒體文件。 顯然,這意味着沒有應用CSS,也沒有圖像加載。

如果人們可以提供一些有關下一步的建議,將不勝感激。

一些可能有用的設置:

# Static files
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/str8red.com/static/'
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

MEDIA_URL = '/resources/'
MEDIA_ROOT = 'media' if DEBUG else '/var/www/str8red.com/resources/'

該錯誤背后的原因是,Django 1.10不再允許您在URL模式中將視圖指定為字符串(例如“ myapp.views.home”)。

嘗試這個,

from django.conf.urls.static import static

if not settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

#If you are in production (means running using nginx, or apache),
# you don't need this setting. Because, the media and static files
# are served by the nginx/apache, instead of Django.
#if settings.DEBUG:
    #urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM