Article From:https://segmentfault.com/q/1010000011046001
Question:

These two days are learning the contents of the flask file. Find a tutorial on the Internet. I knocked it over to realize it. However, it is found that running a report error in pycharm indicates that there is not an extension package. But when I clearly put down the extension package, I ran the program in the terminal and found that it could be completed.Work.
During that time, I tried to copy and paste the tutorial code directly, but it was the same problem. The downloads of the extension package are all done in pycharm.
Related code:
upload.py

import os
from flask import Flask, render_template
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired, FileAllowed
from wtforms import SubmitField

app = Flask(__name__)
app.config['SECRET_KEY'] = 'I have a dream'
app.config['UPLOADED_PHOTOS_DEST'] = os.getcwd()

photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app)  # set maximum file size, default is 16MB


class UploadForm(FlaskForm):
    photo = FileField(validators=[FileAllowed(photos, u'Image only!'), FileRequired(u'File was empty!')])
    submit = SubmitField(u'Upload')


@app.route('/', methods=['GET', 'POST'])
def upload_file():
    form = UploadForm()
    if form.validate_on_submit():
        filename = photos.save(form.photo.data)
        file_url = photos.url(filename)
    else:
        file_url = None
    return render_template('index.html', form=form, file_url=file_url)


if __name__ == '__main__':
    app.run()

index.html

<!DOCTYPE html>
<title>Upload File</title>
<h1>Photo Upload</h1>
<form method="POST" enctype="multipart/form-data">
     {{ form.hidden_tag() }}
     {{ form.photo }}
     {% for error in form.photo.errors %}
         <span style="color: red;">{{ error }}</span>
     {% endfor %}
     {{ form.submit }}
</form>

{% if file_url %}
<br>
<img src="{{ file_url }}">
{% endif %}
Raw

pycharRunning results:

Terminal operation results:

Code run successful web page:

Is there any relevant setup for pycharm to change? Ask for guidance!!!

Answer 0:

It’s pycharm that can’t find flask_uploads.

0.Ensure that flask_uploads exists, and set up the interpreter and running environment of the project in setting.
1.Right click on the appropriate folder on the pycharm directory tree, Mark Directory as – Source Root
2.Try to rewrite the import statement.

Answer 1:

Do you use the right virtualenv in PyCharm?

Answer 2:

I’ve had the same problem with you before, the code is the same, and pycharm reports errors, but it works in the terminal, no errors. I’m going to test the two pieces of code and put them in the txt to compare, and it turns out they’re not the same length, and then it turns out there’s a space problem, so you adjust the pThe space in the ycharm, the change line and the like.

Leave a Reply

Your email address will not be published. Required fields are marked *