zoukankan      html  css  js  c++  java
  • 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/
  • 相关阅读:
    由chkconfig 引发的联想——怎么查看程序是否已经安装/成功安装
    C#反射动态调用dll中的方法,并返回结果
    走过2011年终总结
    Ext.Net系列:二Event之DirectEvents
    缇 、 像素 、 厘米
    Ext.Net系列:二Event之DirectEvent 示例1调用事件顺序
    Ext.Net系列:二Event之DirectEvent 示例2(Delay)
    html符号标签
    ubuntu11.10 安装tplink usb无线网卡 tlwn725n
    GridView 行颜色变化
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/9329169.html
Copyright © 2011-2022 走看看