Flask服务器静态文件未加载

时间:2018-12-11 11:35:05

标签: python flask jinja2 wsgi

我做了一个非常简单的Flask服务器和一个简单的索引,其中包含一些文本和5张图像。图片无法加载,我查看了请求,文件大小与实际图片大小相比非常小。所显示的图像响应大小大约为几KB。该服务器本地托管在通过以太网直接连接到我的笔记本电脑的设备上。

index.html:

    <!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">

        <title>Test Server</title>

    </head>

    <body>

        <h5> HELLO WORLD!</h5>
        <p>This is the server talking</p>
        <p>I am currently sick but I'll be fine soon :)</p>
        <img src="{{ url_for('static', filename='images/a.jpg') }}"></img>
        <img src="{{ url_for('static', filename='images/b.jpg') }}"></img>
        <img src="{{ url_for('static', filename='images/c.jpg') }}"></img>
        <img src="{{ url_for('static', filename='images/d.jpg') }}"></img>
        <img src="{{ url_for('static', filename='images/e.jpg') }}"></img>


        </script>
    </body>

</html>

server.py:

#!/usr/bin/env python2

import gevent
from gevent import monkey

gevent.monkey.patch_all()

from flask import Flask, render_template, url_for, copy_current_request_context, redirect, request, session, send_file, make_response, flash
from flask_socketio import SocketIO, emit
import os

app = Flask(__name__, template_folder="/test-server/current/src/templates/", \
            static_folder="/test-server/current/src/static", \
            static_url_path="/test-server/current/src/static")

basedir = os.path.abspath(os.path.dirname(__file__))

app.config.update(
    SECRET_KEY = 'you-will-never-guess',
)
# Index route
@app.route("/")
@app.route('/home')
def index():

    try:
        nothing = ''

    except Exception as err:
        print(str(err))

    finally:

        response = render_template("index.html")
        return response

# Starts the app listening to port 80
if __name__ == "__main__":
    app.jinja_env.cache = {}
    app.run(host='192.168.16.1',port=80, threaded=True)

这些是我重新加载页面时得到的响应:

enter image description here

0 个答案:

没有答案
相关问题