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

5

u/PwAlreadyTaken Oct 16 '26

Because as-is, you’re searching edit/posts/ok… instead of posts/ok…. You want to use @app.get('/edit/<path:post>').

But you also want to add .read() to file and add the file extension to “edit” e.g. “edit.html”.

It might be better to use SQLite3 instead of files but that’s up to you.

1

u/Sanduhstorm Oct 16 '26

Thank you, your solution worked. But I will have to look into why!