鍍金池/ 問答/Python/ django上傳文件url配置出錯

django上傳文件url配置出錯

再學(xué)習(xí)django 配置上傳文件時,配置url出錯

from django.conf.urls import url
from . import views
from django.conf import settings
from .upload import upload_image

urlpatterns = [
# 首頁
    url(r'^$', views.index, name='index'),
    url(r"^uploads/(?P<path>.*)$", 'django.views.static.serve', {"document_root": settings.MEDIA_ROOT, }),
    url(r'^admin/upload/(?P<dir_name>[^/]+)$', upload_image, name='upload_image'),
]

提示信息是需要列表/元組的格式,好像未發(fā)現(xiàn)格式錯誤,故求助,django版本是1.11

  File "C:\Users\kuangcuisheng\Desktop\cytxt\bbnews\bbnews\***\urls.py", line 13, in <module>
    url(r"^uploads/(?P<path>.*)$", 'django.views.static.serve', {"document_root": settings.MEDIA_ROOT, }),
 
    raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().
回答
編輯回答
安淺陌

自己搞定了,原因是在1.11里 serve() 改變了寫法

from django.conf import settings
from django.views.static import serve

# ... the rest of your URLconf goes here ...

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

來自 https://docs.djangoproject.co...

2018年8月3日 11:53