django2 + python3 顯示靜態檔案中的圖片

來源:互聯網
上載者:User

標籤: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 顯示靜態檔案中的圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.