2017-05-23 167 views
0

我已經從1.9.6升級到Django 1.10。下面是以前工作我的urls.py文件:Django升級:不提供靜態文件

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。服務於'字符串。如果我從字符串和鏈接刪除他們實際的視圖我得到以下錯誤:

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/' 

回答

1

錯誤背後的原因是,Django的1.10不再允許您指定的觀點作爲一個字符串(如「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) 
+0

感謝迅速的迴應。我得到了以上錯誤與上述「名稱」靜態「未定義」 –

+0

從django.conf.utils.urls導入靜態,更新代碼 – zaidfazil

+0

好消息是,該網站加載,但仍然沒有圖像或css正在服務。您是否需要查看我的任何設置? –