简体   繁体   English

图像不会加载到 static 文件夹中的 flask 中

[英]Images won't load in flask in static folder

I'm trying to load two images into my html when the 'next' button is clicked.单击“下一步”按钮时,我正在尝试将两个图像加载到我的 html 中。 Before I tried to connect all of my front end to flask, it worked fine.在我尝试将所有前端连接到 flask 之前,它工作正常。 The css and js files are working just fine, but the images won't load. css 和 js 文件运行良好,但无法加载图像。 What happens is when the button is pressed, the js swaps out the old html in the main div with the new, which contains images (you can see this in the update() function).发生的情况是当按下按钮时,js 将主 div 中的旧 html 换成新的,其中包含图像(您可以在 update() 函数中看到这一点)。 All of the js, css and images are in the static folder.所有的 js、css 和图像都在 static 文件夹中。 Any help would be appreciated.任何帮助,将不胜感激。 Edit: extra info: It's throwing a 404 error saying it cannot locate the images in a folder called Images, even though I'm not asking it to look for them in that, I'm only asking it to look in the static folder.编辑:额外信息:它抛出一个 404 错误,说它无法在名为 Images 的文件夹中找到图像,即使我没有要求它在其中查找它们,我只是要求它在 static 文件夹中查找。 github repo: https://github.com/ewanh26/Web-App Code: github repo: https://github.com/ewanh26/Web-App代码:

main.py:

from flask import *
    
app = Flask(__name__)
    
@app.route("/")
def index():
    return render_template('index.html')
    
if __name__ == '__main__':
    app.run(debug=True)

index.js:

<!-- language: lang-js -->

    let pageCounter = 0;
    let maxPages = 5

    const $prevButton = document.getElementById('prev')
    const $nextButton = document.getElementById('next');
    const $div = document.getElementById('maindiv')

    function update() {    
        switch (pageCounter) {
            default:
                $div.className = 'slide1Header';
                $($div).html("<h1>THE WORLD OF TOMORROW. WHAT'S NEXT FOR US?</h1>");
                break;
            case 1:
                $div.className = 'slide2';
                $($div).html(`
                <img src="{{ url_for('static', filename='starry.jpg') }}" alt="stars" id="starimg">
                <img src="{{ url_for('static', filename='moon.png') }}" alt="moon" id="moonimg">
                `);
                break;
        }
    }

    update();

    $prevButton.addEventListener('click', function () {
        if (pageCounter > 0) {
            pageCounter--;
            console.log(pageCounter);
            update();
        }
    });
    $nextButton.addEventListener('click', function () {
        if (pageCounter < maxPages) {
            pageCounter++;
            console.log(pageCounter);
            update();
        }
    });

    window.addEventListener('scroll', function () {
        document.body.style.setProperty('--scroll', window.pageYOffset / (document.body.offsetHeight - window.innerHeight));
    });

style.css
<!-- language: lang-css -->

    html, body {
        font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;
        margin: 0;
        padding: 0;
        height: 100%;
        width: 100%;
    }
    .slide1Header {
        display: flex;
        justify-content: center;
        align-items: center;
        position: fixed;
        font-style: italic;
        width: 100%;
        height: 100%;
        background: linear-gradient(-45deg,
        #bd33c4,
        #DE498E, 
        #FF5F58, 
        #FC9144, 
        #f8c330e1, 
        #FBAB19, 
        #FD9301, 
        #FE4A2B, 
        #FF0055
        );
        background-size: 2000% 2000%;
        animation: backgroundChange 15s ease infinite;
        transform: none;
    }
    .slide2 {
        background: black;
    }
    #starimg {
        display: block;
        width: 50%;
        margin-left: auto;
        margin-right: auto;

    }
    #moonimg {
        display: block;
        width: 30px;
        height: 30px;
        top: 17px;
        right: 33%;
        left: 66%;
        position: absolute;
        filter: brightness(5);
        filter: contrast(0);
        filter: blur(0.7px);
        filter: opacity(0.95);
        animation: moonScroll 0.5s linear infinite;
        animation-play-state: paused;
        animation-delay: calc(var(--scroll) * -1s);
        transform: rotate(-40deg);
        transform-origin: 0px 500px;
    }
    #moonimg::before {
        transform: rotate(-30deg);
    }
    button.prev, button.next {
        border-radius: 8px;
        position: fixed;
        transition-duration: 0.4s;
        border-color: white;
        border-style: none;
        padding: 4pt;
    }
    button.prev {
        left: 5%;
        bottom: 5%;
    }
    button.next {
        right: 5%;
        bottom: 5%;
    }
    .prev:hover, .next:hover, .prev:focus, .next:focus {
        background-color: #ff245b;
        color: white;
        outline: none;
    }
    @keyframes backgroundChange {
        0% {
            background-position: 0 50%;
        }
        50% {
            background-position: 100% 50%;
        }
        100% {
            background-position: 0 50%;
        }
    }
    @keyframes moonScroll {
        from {
            transform: rotate(0deg);
        }
        to {
            transform: rotate(-90deg);
        }
    }

index.html
<!-- language: lang-html -->

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
            <!--<link rel="preload" as="image" href="{{ url_for('static', filename='starry.jpg') }}">
            <link rel="preload" as="image" href="{{ url_for('static', filename='moon.png') }}">!-->
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
            <title>The Future</title>
        </head>
        <body>
            <div id="maindiv"></div>
            <button class="prev" id="prev">&larr; Previous</button>
            <button class="next" id="next">Next &rarr;</button>
            <script src="{{ url_for('static', filename='index.js') }}"></script>
        </body>
    </html>

index.js

    let pageCounter = 0;
    let maxPages = 5
    
    const $prevButton = document.getElementById('prev')
    const $nextButton = document.getElementById('next');
    const $div = document.getElementById('maindiv')
    
    function update() {    
        switch (pageCounter) {
            default:
                $div.className = 'slide1Header';
                $($div).html("<h1>THE WORLD OF TOMORROW. WHAT'S NEXT FOR US?</h1>");
                break;
            case 1:
                $div.className = 'slide2';
                $($div).html(`
                <img src="{{ url_for('static', filename='starry.jpg') }}" alt="stars" id="starimg">
                <img src="{{ url_for('static', filename='moon.png') }}" alt="moon" id="moonimg">
                `);
                break;
        }
    }
    
    update();
    
    $prevButton.addEventListener('click', function () {
        if (pageCounter > 0) {
            pageCounter--;
            console.log(pageCounter);
            update();
        }
    });
    $nextButton.addEventListener('click', function () {
        if (pageCounter < maxPages) {
            pageCounter++;
            console.log(pageCounter);
            update();
        }
    });
    
    window.addEventListener('scroll', function () {
        document.body.style.setProperty('--scroll', window.pageYOffset / (document.body.offsetHeight - window.innerHeight));
    });

Managed to fix it myself just by using the normal file path rather than creating a url with url_for() in the javascript file.仅通过使用普通文件路径而不是在 javascript 文件中创建带有 url_for() 的 url 来管理自己修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM