django2 + python3 显示静态文件中的图片
之前一直搞不出来 是因为图片的问题,步骤也就是固定的几步,到位了就差不多成了
文件夹结构:
. ├── 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 render from django.shortcuts import render from django.http import HttpResponse def showImg(request): return render(request,'index.html')
urls.py
from django.contrib import admin from django.urls import path from hello import views from django.conf.urls.static import static from . import settings urlpatterns = [ 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>
此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935.
我的gitHub: (学习代码都在gitHub)
https://github.com/nwgdegitHub/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现