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/
  • 相关阅读:
    PHP 页面编码声明方法详解(header或meta)
    PHP error_reporting() 错误控制函数功能详解
    发送邮件程序报错454 Authentication failed以及POP3和SMTP简介
    使用 PHPMailer 发送邮件
    PHP debug_backtrace() 函数
    PHP_php.ini_说明详解
    详解spl_autoload_register()函数
    各浏览器对常用或者错误的 Content-Type 类型处理方式不一致
    string.format大全
    ASP.NET MVC如何实现自定义验证(服务端验证+客户端验证)
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/9329169.html
Copyright © 2011-2022 走看看