-1

I think all settings are correct but image cannot be uploaded: When upload an image, the warning for uploading notes shows up --- Does that mean uploading image cannot be executed?

In other words, everything displayed correctly. BUT when I upload image, the warning says "This cannot be a real anime name!", which is the warning for notes. What could be the issue?

(This is models.py)

    class Img(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text, unique = True, nullable = False)
    img = db.Column(db.Text, unique = True, nullable = False)
    mimetype = db.Column(db.Text, nullable = False)
    date = db.Column(db.DateTime(timezone=True), default=func.now())
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    

class Note(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    data = db.Column(db.String(10000))
    date = db.Column(db.DateTime(timezone=True), default=func.now())
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))


class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(150), unique=True)
    password = db.Column(db.String(150))
    first_name = db.Column(db.String(150))
    notes = db.relationship('Note')
    img = db.relationship('Img')

(This is from home.html)
<ul class="list-group list-group-flush" id="imgs">
      {% for img in user.imgs %}
      <li class="list-group-item">
        {{ img.data }}
        {% endfor %}
    <form method="POST">
        <label for="pic"></label>
        <br />
      <div align="center">
        <input type="file" class="form-control- file" name='img' id="img">
        <button type="submit" class="btn btn-danger">Upload image</button>
      </div>
    </form>

    {% endblock %}


  (This is views.py)
@views.route('/', methods=['GET', 'POST'])
@login_required
def home():
    if request.method == 'POST':
        note = request.form.get('note')

        if not note:
            flash('This cannot be a real anime name!', category='error')
        else:
            new_note = Note(data=note, user_id=current_user.id)
            db.session.add(new_note)
            db.session.commit()
            flash('Added!', category='success')

    return render_template("home.html", user=current_user)


@views.route('/delete-note', methods=['POST'])
def delete_note():
    note = json.loads(request.data)
    noteId = note['noteId']
    note = Note.query.get(noteId)
    if note:
        if note.user_id == current_user.id:
            db.session.delete(note)
            db.session.commit()

    return jsonify({})

@views.route('/', methods=['GET', 'POST'])
@login_required
def upload():
    if request.method == 'POST':
        pic = request.files['pic']

        if not pic:
            flash('You uploaded nothing!', category='error')
        else:
            filename = secure_filename
            mimetype = pic.mimetype
            img = Img(img=pic.read(),mimetype=mimetype,name=filename, user_id=current_user.id)
            db.session.add(img)
            db.session.commit()
            flash('Uploaded!', category='success')

    return render_template("home.html", user=current_user)

1 Answers1

-1

In order for this to work, wouldn't you need to change the name of the file input to 'note'?

note = request.form.get('note')

Try making the following changes in home.html

<input type="file" class="form-control- file" name='note' id="img">
Andrew Clark
  • 850
  • 7
  • 13