简体   繁体   中英

view didn't return a response in flask

I'm new to flask. I'm trying to create two upload request: 1. first file will save no matter 2. if i don't upload second file, it will help me to generate output.json. Can anyone suggest why haven't I return a valid response? Thanks

TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

route:

@app.route('/upload', methods=['POST','GET'])
def upload():
if request.method == 'POST' and 'txt_data' in request.files:

    #Files upload and initiation
    num_sentences = int(request.form['num_sentences'])
    session["num_sentences"] = num_sentences
    uploaded_file = request.files['txt_data']
    filename = secure_filename(uploaded_file.filename)
    session["filename"] = filename 
    
    # save uploaded file
    if filename != '':
        uploaded_file.save(os.path.join(app.config['UPLOAD_PATH'], filename))

    text_file = open(os.path.join('uploads',filename), "r").read()
    nlp_file_doc = nlp(text_file)
    all_sentences = list(nlp_file_doc.sents)
    ongoing_sentences = all_sentences[num_sentences:]
    first_sentence = ongoing_sentences[0].text

    # Save output file 
    if 'output_data' in request.files:
        output_file = request.files['output_data']
        output_filename = secure_filename(output_file.filename)
        uploaded_file.save(os.path.join(app.config['OUTPUT_PATH'], output_filename))
    else:
        data = {first_sentence:[]}
        with open("output.json", "w") as write_file:
            json.dump(data, write_file)

    #Test out the first sentence
    extraction = apply_extraction(first_sentence, nlp)
    return render_template('annotation.html', 
                            all_sentences = all_sentences, 
                            extraction = extraction, num_sentences = num_sentences)

html:

<form method=POST enctype=multipart/form-data action="{{ url_for('upload') }}" class="form-group">
                    <div class="form-group">
                      <input type="file" name="txt_data">
   
                      <form method=POST enctype=multipart/form-data action="{{ url_for('upload') }}" class="form-group">
                        <div class="form-group">
                          <input type="file" name="output_data">
                        </form>

Your route accepts both GET & POST methods but returns only in the case you have a POST request.

@app.route('/upload', methods=['POST','GET'])
def upload():
    if request.method == 'POST':
        ...
        return something
    # What happens here if the request.method is 'GET'?

If you make a GET request on the /upload, then the function will return nothing, throwing error.

You can either remove the GET method or return something for the GET case.

Solution 1:

@app.route('/upload', methods=['POST'])

Solution 2:

@app.route('/upload', methods=['POST','GET'])
def upload():
    if request.method == 'POST':
        return something
    return something_else # if request.method == 'POST' returns false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM