標籤:python3 檔案 mod 本地 height set show turn 無法
之前一直搞不出來 是因為圖片的問題,步驟也就是固定的幾步,到位了就差不多成了
檔案夾結構:
.├── HelloWorld│ ├── __init__.py│ ├── __pycache__│ │ ├── __init__.cpython-36.pyc│ │ ├── settings.cpython-36.pyc│ │ ├── urls.cpython-36.pyc│ │ └── wsgi.cpython-36.pyc│ ├── settings.py│ ├── urls.py│ └── wsgi.py├── db.sqlite3├── hello│ ├── __init__.py│ ├── __pycache__│ │ ├── __init__.cpython-36.pyc│ │ ├── admin.cpython-36.pyc│ │ ├── models.cpython-36.pyc│ │ └── views.cpython-36.pyc│ ├── admin.py│ ├── apps.py│ ├── migrations│ │ ├── __init__.py│ │ └── __pycache__│ │ └── __init__.cpython-36.pyc│ ├── models.py│ ├── static│ │ └── hello│ │ └── 6.png│ ├── templates│ │ └── index.html│ ├── tests.py│ └── views.py└── manage.py
settings.py檔案加這兩句
STATIC_URL = ‘/static/‘MEDIA_ROOT = os.path.join(BASE_DIR, ‘static/‘)
views,py檔案加
from django.shortcuts import renderfrom django.shortcuts import renderfrom django.http import HttpResponsedef showImg(request): return render(request,‘index.html‘)
urls.py
from django.contrib import adminfrom django.urls import pathfrom hello import viewsfrom django.conf.urls.static import staticfrom . import settingsurlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘‘,views.showImg)]
index.html檔案
一定要寫
{% load staticfiles %} 這句
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> {% load staticfiles %}</head><body><h1>顯示一張本地圖片</h1><img src="{% static ‘6.png‘ %}" width="500" height="500" alt="圖片無法顯示"></body></html>
django2 + python3 顯示靜態檔案中的圖片