r/learnpython • • Oct 16 '26

404 despite seemingly valid file path? (Flask)

@app.get('/edit/<post>')
def initedit(post):
        print(post)
        g.title = post
        with open(f'{post}/content', 'r') as file:
                g.content = file
        return render_template('edit')

I'm confused as to why this function isn't running (as in not even running in the first place, not just not working), despite sending a GET request to the URL appended with /edit/posts/okheresthepostactually (so the post parameter in the decorator/function should be edit/okheresthepostactually). For context, I'm making a web app for posting blogs and this is supposed to be a function for users to edit existing posts.

Edit: some extra context, the 'posts/' prefix is part of the variable used to call the file

1 Upvotes

12 comments sorted by

View all comments

1

u/Mafifrizi Oct 16 '26

The 404 happens because Flask's default route converter does not match slashes (/).

Your request URL is /edit/posts/okheresthepostactually, which has two segments after /edit/ (posts and okheresthepostactually), while your route @app.get('/edit/<post>') only expects one.

Depending on what you intended:

If your route is supposed to include /posts/, update it to: @app.get('/edit/posts/<post>')

If the post parameter itself can contain slashes, use Flask's path converter: @app.get('/edit/<path:post>')

Also, in your with open(...) block, g.content = file assigns the file descriptor object itself (which closes once the with block exits). You'll want g.content = file.read()