轉自:https://blog.csdn.net/thy38/article/details/23553975
通常在應用的根目錄下建立兩個目錄
1. static用來存放網站自己的圖片,js,css等
2. media用來存放用戶上傳的圖片,文件等
現在假設有d:/wwwsite/office/static/images/psb.jpg
在urls.py中的urlpatterns中添加:
urlpatterns = patterns('',
url(r'^static/(?P<path>.*)', 'django.views.static.serve', {'document_root': 'd:/wwwsite/office/static'}),
)
在瀏覽器中輸入:127.0.0.1:8000/static/images/psb.jpg即可
在html中引用方法
<img src="/static/images/psb.jpg>即可
django在解析到地址為http://localhost/static/images/psb.jpg時會將其解析為d:/wwwsite/office/static/images/psb.jpg