zmm064
3/31/2018 - 10:46 AM

在调试模式下使用django处理静态文件

在调试模式下使用django处理静态文件

"""
Definition of urls for DjangoByExample_2_SocialWebsite.
"""

from django.conf.urls import include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^account/', include('account.urls')),
]

if settings.DEBUG:
    # 在调试模式下django负责服务静态文件,这里应该是负责处理用户上传的静态文件才会用得到
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    # 类似的,当你想为静态文件分配多个不同的网址时,也可以这么写
    # urlpatterns += static('/media2/', document_root=media_root)
from django.conf import settings
from django.views.static import serve

# ... the rest of your URLconf goes here ...
if settings.DEBUG:
    urlpatterns += [
        # Note, the snippet assumes your MEDIA_URL has a value of '/media/'.
        url(r'^media/(?P<path>.*)$', serve, {
            'document_root': settings.MEDIA_ROOT,
        }),
    ]
# Since it can become a bit cumbersome to define this URL pattern, 
# Django ships with a small URL helper function static(prefix, view=django.views.static.serve, **kwargs) 

from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)