Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

Flask is not redirecting - Python

I am developing an app using Flask, In the app, user upload files to the server, before uploading I used to check that user is authenticated or not. If the user is authenticated, then the uploading file is saved in the server otherwise flask redirected to home page.

Code - In app.py -

@login_required
@app.route('/home')
def home():
    if current_user.Is_Authenticated:
        return redirect(url_for('post1'))
    else:
        return render_template('post2.html')

@app.route('/upload', methods=['POST'])
def upload():
    if current_user.Is_Authenticated:
        user = current_user.Email
        flag = True
    else:
        print("Current User is not Authenticated")
        flag = False
    if(flag):
        if(request.method == "POST"):
            if(request.files['myfile']):
                myfile = request.files["myfile"]
                
                sfname = os.path.join(os.getcwd(), 'static', str(secure_filename(myfile.filename)))
                myfile.save(sfname)
                return render_template('post.html')
    else:
        return redirect(url_for('home'))

When, I am testing the app, I found that, before authenticated, a user upload files with larger than 10MB, it shows "Site is not reachable". If it is small sized file, then flask redirected to home page, correctly.

how to solve this, why flask is not working when the uploading file size is larger than 10Mb, Thank you

question from:https://stackoverflow.com/questions/65625852/flask-is-not-redirecting-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Flask you can limit the file size using MAX_CONTENT_LENGTH while configuring your app.

If the file size is greater than MAX_CONTENT_LENGTH you get this kind of errors Please refer to this (for error messages, for different browser and there are also solutions as well)

Increase this MAX_CONTENT_LENGTH to handle large file size

or use the below code to handle large file error

@app.errorhandler(413)
def request_entity_too_large(error):
    return 'File Too Large', 413

Happy coding!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...