程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django has two ways to access pictures

編輯:Python

Access pictures in static resources ( Just put it in static Pictures in the folder )

Static resources store some pages that need to be loaded frequently and are generally not defined by users , So it will not change where it is stored , The folder name is defined as static, Position in Yours app Next

access :

stay settings.py The configuration at the bottom of the file

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
Type in the browser https://host:port/static/img.png   You can access the... In your static resources folder img.png picture

When we have a lot of pictures , If you also put it in static Under the folder , The system will be overloaded , The operation is not smooth and even crashes , So it is necessary to define a folder for storing pictures , These pictures will not be loaded into memory as the program runs , It is called only when accessing .

access :

Define a routing function

def ilcimg(request):
    imagepath = "photo/123.png"
    image_data = open(imagepath,"rb").read() 
    return HttpResponse(image_data, content_type="image/png")
 
By configuring to urls.py in , Access to url Address to get the picture

stay doc root directories creating photo/ Deposit 123.png, then url.py It specifies the access routing function . 

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved