1.在setting中进行配置:
(1)在INSTALLED_APPS 中添加’django.contrib.staticfiles’,
(2)新增
STATIC_URL = '/home/test/Desktop/'
STATICFILES_DIRS=[os.path.join(BASE_DIR,"/home/test/Desktop/")]
注:其中的在INSTALLED_APPS 中添加'django.contrib.staticfiles'和
STATIC_URL = '/home/test/Desktop/'可以不要!!!
2.在urls.py文件中添加
from django.views.static import serve
url(r'^static/(?P<path>.*)$', serve, {'document_root': /home/test/Desktop/ '})
注意document_root后面的地址要与第一步注册的地址相同!!!
3.启动服务器,输入地址192.168.0.1:8080/book/009.jpg,即可访问!
注:第一步配置的地址'/home/test/Desktop/'是你存放图片的地址;
上述操作所在的app为主app;
注意:图片的读取需要用r+,w+;
with open("/home/Desktop/22.jpg","rb") as file:
print("11111")
a=file.read()
print("222222")
with open("static/4.jpg","wb") as file1:
file1.write(a)
这段代码可以将外部图片写入项目中;