2017年1月5日 星期四

[Django]靜態文件設置

如果想要讓Django網站上的圖片顯示出來,最正規的方法
1.修改urls.py


from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$',homepage),
    url(r'^post/(\w+)$',showpost),
]+static(settings.STATIC_URL, document_root=settings.STATIC_URL)

#讓django可以偵測網址結尾為http://127.0.0.1/static/..........

2.修改settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static/images'),
]

STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')

#STATIC_URL 讓 templates 裡的網址不會寫死
#STATICFILES_DIRS 執行collectstatic 時django會去這些資料夾收集檔案
#STATIC_ROOT 執行collectstatic django會把檔案集中到此目錄

3.模板渲染的修改
{% load staticfiles %}
<img src="{% static " swift.jpg="" />

4.執行python manage.py collectstatic

http://www.jianshu.com/p/a3fb31f49f2e
http://eric0806.blogspot.tw/2014/04/blogger-google-code-prettify.html
https://docs.djangoproject.com/en/1.10/howto/static-files/

沒有留言:

張貼留言