Django升级:未提供静态文件

时间:2017-05-23 07:07:14

标签: python css django

我已从1.9.6升级到Django 1.10。下面是我以前工作过的urls.py文件:

completed

当我尝试运行网站时,我得到了:

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}),
    ]

我知道上面的错误是由于'django.views.static.serve'在字符串中。如果我从字符串中删除它们并链接到实际视图,我会收到以下错误:

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

如果我删除“Get Django以调试模式提供媒体文件”中的所有内容。并且在网站下方加载但不提供静态或媒体文件。这显然意味着没有应用css并且没有加载图像。

如果人们可以就接下来的步骤提出一些建议,那将是值得赞赏的。

一些可能有用的设置:

name 'django' is not defined

1 个答案:

答案 0 :(得分:1)

错误背后的原因是,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)