Forums

How to Upload Photos

I am currently trying to have users upload photos to my webpage and then when they click submit, have those photos go to a folder in my directory. I keep getting the error in my error.log: 2019-12-02 20:18:35,954: "The view function did not return a valid response. The" Has anyone got this error before? I have tried everything but I am unable to fix this issue!

Python Code:

from flask import Flask, flash, redirect, render_template, request, url_for, send_from_directory
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = '/home/HassanSherien/mysite/Shape_Image/'
def allowed_file(filename):
     return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
#@app.route('/upload', methods=['GET', 'POST'])
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
              flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):

            filename = secure_filename(file.filename)
            flash(file)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',filename=filename))

        #elif request.method == "GET":
            #return render_template("overview_main_page.html", comments=Comment.query.all())
    #return

@app.route('/uploads/<filename>', methods=["GET", "POST"])
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename)
<!-- Html Code Below -->
     <div class="w3-panel">
      <div class="w3-row-padding" style="margin:0 -16px">
      <div class="w3-third">
       <form action="{{ url_for('upload_file') }}" method="post">
       <!-- <form action="{{ url_for('upload_file') }}" method='POST'> -->
       <input type="file" name="file" value="fileupload" id="file_upload">
       <input type='submit' />

the response for GETs is commented out and not returning anything? also it looks one level too indented?

Even when I keep the GET response and return, I get the error 2019-12-03 14:29:37,895: "The view function did not return a valid response. The" Also, my html code is properly formatted! I just couldn't format it in the comment above.

It's not the HTML code's formatting that Conrad was asking about -- it's the Python code. You have this:

@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
              flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):

            filename = secure_filename(file.filename)
            flash(file)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',filename=filename))

        #elif request.method == "GET":
            #return render_template("overview_main_page.html", comments=Comment.query.all())
    #return

This bit:

        #elif request.method == "GET":
            #return render_template("overview_main_page.html", comments=Comment.query.all())

...is indented so that it is inside the

if request.method == 'POST':

block. It needs to not just be uncommented, it also needs to be dedented by four spaces.

Oh I am sorry for misunderstanding! The section I will now uncomment needed to be on the same line as my if request.method== 'POST'

I see now that it is not giving any error anymore(thank you for that!) but now my follow up question is, where are these photos going? I thought I set it so that it enters into /home/HassanSherien/mysite/Shape_Image which is a folder I created inside pythonanywhere itself.

These seem like coding problems- perhaps you should get more help from say a coding mentor.

I tried to get help from coding mentor but my tutor told me it was something on the pythonanywhere end and that I was possibly not granted permission to save photos onto a folder in the interface itself.

If it were a permission error, you'd see an error message when you try to upload the file -- are you getting that?

Ah I see yes I am. Thank you for the help!

No problem, glad to help!