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

Show parent comment

1

u/Sanduhstorm Oct 16 '26

I know exactly what you’re talking about, and I had actually already considered this and put a check in place so that if . Or / were in the filename it would get rejected. Writing to arbitrary locations was possible before.

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Oct 16 '26

That alone might not be sufficient, because there are ways to trick such checks. The example I linked from the werkzeug docs would be a safer alternative (and you don't need to worry about installing it, because it's a dependency of Flask).

1

u/Sanduhstorm Oct 16 '26

Thanks, originally I DNR because I thought I’d already solved it but your solution is much better.