title: Python + Flask Image Upload "No file part"
body: <p>I am new to python/flask and am trying to upload an image for a users profile picture during registration.  I've taken the backend flask code related to file uploads directly from their documentation found <a href="http://flask.pocoo.org/docs/0.12/patterns/fileuploads/" rel="nofollow noreferrer" title="here">here</a>.  When I perform a registration, I receive the HTTP error: </p>
  
  <blockquote>
    <p>Bad Request The browser (or proxy) sent a request that this server
    could not understand.</p>
  </blockquote>
  
  <p>And in my python console:</p>
  
  <blockquote>
    <p>no file part</p>
  </blockquote>
  
  <p>I am using a bootstrap fork "Upload Image w Preview &amp; Filename" found <a href="https://bootsnipp.com/snippets/eNbOa" rel="nofollow noreferrer" title="here.">here</a>.  Here is my registration form:</p>
  
  <pre><code>&lt;form action="/register" method="POST"&gt;
    &lt;div class="form-group"&gt;
      &lt;label for="username"&gt;Username&lt;/label&gt;
      &lt;input type="text" class="form-control" name="username" id="username" placeholder="Username"&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label for="password"&gt;Password&lt;/label&gt;
      &lt;input type="password" class="form-control" name="password" id="password" placeholder="Password"&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label for="confirm-password"&gt;Confirm Password&lt;/label&gt;
      &lt;input type="password" class="form-control" name="confirm-password" id="confirm-password" placeholder="Confirm Password"&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
        &lt;label&gt;Profile Picture&lt;/label&gt;
        &lt;div class="input-group"&gt;
            &lt;span class="input-group-btn"&gt;
                &lt;span class="btn btn-default btn-file"&gt;
                  &lt;button class="btn btn-success"&gt;Browse…&lt;/button&gt;
                    &lt;input type="file" id="imgInp" name="file"&gt;
                &lt;/span&gt;
            &lt;/span&gt;
            &lt;input type="text" class="form-control" readonly&gt;
        &lt;/div&gt;
        &lt;img id='img-upload' style="margin-top: 10px;"/&gt;
    &lt;/div&gt;
    &lt;button type="submit" class="btn btn-primary"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And here is my backend/flask stuff.</p>
  
  <pre><code>from cs50 import SQL
  from flask import Flask, flash, redirect, render_template, request, session, url_for
  from flask_session import Session
  from tempfile import mkdtemp
  from werkzeug.exceptions import default_exceptions
  from werkzeug.security import check_password_hash, generate_password_hash
  from werkzeug.utils import secure_filename
  import datetime
  import os
  
  UPLOAD_FOLDER = '/static/images'
  ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
  
  app = Flask(__name__)
  app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
  app.secret_key = "super secret key"
  app.config["SESSION_TYPE"] = "filesystem"
  db = SQL("sqlite:///data.db")
  Session(app)
  
  @app.route('/register', methods=['POST'])
  def register():
      username = request.form.get("username")
      password = request.form.get("password")
      row = db.execute("SELECT * FROM users WHERE username = :username", username=username)
      if not row:
          if 'file' not in request.files:
              flash('No file part')
              print("no file part")
          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')
              print("no selected file")
          if file and allowed_file(file.filename):
              filename = secure_filename(file.filename)
              file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
              return redirect(url_for('uploaded_file',
                                      filename=filename))
          db.execute("INSERT INTO users (username, passwordHash, pictureUrl) VALUES (:username, :passwordHash, 'static/images/default.jpg')", username=username,passwordHash=generate_password_hash(password))
      else:
          return apology("Username already exists.")
      return redirect("/")
  </code></pre>