Error Retrieving Video File in Flask: 404 Not Found Issue

I'm having trouble with displaying static files on my Flask website and would really appreciate some help since I don't have much experience in web development.

Sorry if I'm not explaining clearly, but here's the issue:

Problem: Whenever I try to load static files (specifically an mp4 video) on my Flask site, I constantly get a 404 error. Despite checking file paths and permissions, I can't seem to fix the problem. I know there are many similar questions about serving static files, but the solutions provided haven't worked for me, especially since I just want to add a background video to my site.

Here are the relevant code snippets from my Flask app:

Setting up Flask application

app = Flask(__name__)
app._static_folder = '/static'

HTML/CSS for background video

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Barcode Wall Control Server</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="670508081314131506172752495449574a060b170f0656">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        @media screen and (max-height: 450px) {
            .sidebar {padding-top: 15px;}
            .sidebar a {font-size: 18px;}
        }

        #mainContent {
        padding: 20px;
        }

        /*Video Background */
        #backVideo {
            position: fixed;
            right: 0;
            bottom: 0;
            min-width: 100%;
            min-height: 100%;
            width: auto;
            height: auto;
            z-index: -1;
            overflow: hidden;
        }

    </style>
</head>
<body style="background-color: #b8b7b74f;">

    <video autoplay loop muted id="backVideo">
        <source src="{{url_for('static', filename='barcode.mp4')}}" type="video/mp4">
    </video>
</body>  

When I try to load the static files, my terminal shows this error message:

[15/May/2024 14:38:46] "GET /static/barcode.mp4 HTTP/1.1" 404

PS. The HTML code works fine when run without the server.

Answer №1

It seems like there are a couple of issues to address here. Firstly, the version of Flask you're using should be specified for clarity. I don't recall ever seeing an option to set the static path with _static_folder (with an underscore prefix).

To configure it properly, you can use the static_folder property of the Flask instance or pass a static_folder=... argument to the Flask constructor.

Another aspect to consider is that the path should either be relative or an absolute path (e.g., /static on your file system), which I assume is not what you're aiming for -- although it does work. According to the documentation, it should be "Relative to the application root_path or an absolute path."

To rectify this, you may want to update your code to something like:

app = Flask(__name__, static_folder="static") 
# or 
# app.static_folder = "static"

Furthermore, keep in mind that static is the default setting. If this isn't functioning as expected, ensure the static folder is correctly located based on the application's root_path. For instance,

├── app
│   ├── __init__.py
│   ├── routes.py
│   └── *static*

To troubleshoot why static assets aren't getting served, try running the following script after initializing your Flask instance:

print(app.root_path)
print(app.has_static_folder)
print(app.static_folder)
import os
names = os.listdir(os.path.join(app.static_folder))
print(names) # list of static files

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Codeigniter: How Ajax handles data transmission without sending any information

I'm encountering an issue with AJAX. I'm attempting to send a value via AJAX to my upload function before submission. However, when I check the $_POST array in my PHP code, only the form value is present and not from the AJAX request. I'm un ...

Adding Content Dynamically Before an HTML Element with CSS During Runtime

Let's say the HTML is defined as <div> <div class="runtime"> Some Important Text.Insert "Important" Before This </div> <div class="normal"> General Text </div> </div> Typically, t ...

Center the content within the div

Is there a way to center the content of the second column within this div? I've tried various methods without success so far. I'm currently using Bootstrap 4. Can anyone provide guidance on how to achieve this? <div class="card"> < ...

The background color is not extending to the edges of the row or column

After making adjustments to the layout of a specific section on my webpage by adding rows and columns, I noticed a blank strip where the background color fails to fill in. The rows within this section are contained within a div class called container-fluid ...

What is the reason behind the addition of the prefix 'ctl00_ctl00' to my ASP.NET page's HTML element IDs, causing a disruption in the design aesthetics?

I'm struggling to grasp this concept. The HTML elements in the master page have their ids changed with a prefix, which is causing a disruption in the CSS layout. In the master page, I currently have: <div id="container" runat="server"> & ...

Incorporating personalized CSS styles into a Bootstrap 4 card

I have been scouring through countless questions and solutions on StackOverflow, but nothing seems to be working for me. While p-5 is doing its job just fine for padding, it applies to all devices. I am in need of specific CSS for mobile devices when it co ...

tips on toggling div visibility based on data filtering

<script> function toggleContent(str) { if(str == 'recent' || str == 'old' && !($(".main_content").is(':visible'))) { $(".main_content").show(); } else if( str == 'newComment') { ...

PHP - Unexpected behavior while using a switch statement for auto-incrementing variable inside a while loop

Can anyone assist me with this problem? I am currently testing a switch statement inside a while loop that compares values. The loop should end once a specific value reaches the limit $numRow, stopping the execution of the remaining code. Here is my code ...

Menu options arranged vertically, revealing submenus that fly out upon mouse hover

I am attempting to develop a vertical menu with flyouts, which includes sub-menus. Can you point out any issues in the code provided below? <html> <head> <title>Untitled Document</title> <style type="text/css ...

Device identification verification similar to that of Google and Facebook

Is there a way to uniquely identify a user's device during authentication so that a second factor of authentication (like SMS) can be required if the user is logging in from an unfamiliar device? Both Google and Facebook have this feature, and it does ...

Guide on playing two HTML5 videos simultaneously once they have been loaded

Is it possible to make two HTML5 videos autoplay simultaneously? Is there a way to delay the playback until both videos have fully loaded? I'm looking to ensure that neither video starts playing before the other has finished loading. Does that clarif ...

Troubleshooting: Resolving the error message "SyntaxError: Unexpected token '<'"

I am currently facing an issue with my code that is supposed to use LAT&LONG data saved in a txt file and display it as a Google Map in HTML. However, the PHP function to import the txt file is not working at all. Any suggestions on what might be causi ...

What is the best way to remove CSS styling from a select element?

My select element is displaying blank option values no matter what I do. The dropdown has the correct number of options, but they are all empty. Here's the HTML snippet: <label for="accountBrokerageName">Brokerage:</label> <se ...

Constructor for 'dojox.mobile.RoundRectCategory' could not be located

Using Worklight 5.06 and Dojo 1.8, I encountered the following errors on the browser's console after an Eclipse crash: Error: Unable to resolve constructor for 'dojox.mobile.RoundRectCategory' Error: Left list not found Error: this.leftLis ...

What is the best way to place a horizontal line behind buttons in a design?

Hello, I was experimenting with creating a background line behind the buttons on my webpage. Previously, I used an image for this purpose but now I want to achieve the same effect using a line. <div class="feedback-option"> <div class="radios2" ...

An Xpath expression that functions perfectly in Firefox isn't working as expected in Selenium

I have a question regarding xpath. In Chrome, there is a td element with the following content: <td class="dataCol col02"> "Hello world(notes:there is$)nbsp;" <a href="xxxx">[View Hierarchy]</a> </td> However, when I inspect the s ...

The background color is not displaying correctly on the <div> element

body { background-color: #FFFDEC; } .header { position: fixed; top: 0; left: 0; height: 50px; width: 100%; background-color: #04A7A6; margin: 0; display: block; z-index: 10; } .headermenu { xposition: fixed; ...

What is the best way to anchor the components to a specific location on the screen in ASP.NET?

Currently I am working on creating a registration page in asp.net. I have been using panels to group the components such as labels, dropdown lists, and text boxes. However, when I run the page, I noticed that the positions of these components keep changing ...

Create a dynamic webpage where two div elements glide in opposite directions

My apologies for this seemingly basic question, but I am wondering if anyone knows how to create an animation where a div moves up and down while scrolling, similar to the effect on this website: On that site, near the footer, there are 2 sections with 2 ...

Firefox not responding to mouse movements

I am experimenting with creating an "animation" using JavaScript's "mousemove" Check it out here This is the code I am currently using $("body").mousemove(function(e){ var wWidth = $("body").width()/2 var wHeight = $("body").height()/2 var posX; v ...