Создайте форму редактирования с ранее заполненными полями, столкнувшись с трудностями при вводе [file] и textarea - PullRequest
0 голосов
/ 26 марта 2019

Я делаю проект Python Flask, пытаюсь «отредактировать» текущий «объект» из postgresql через FlaskForm.До сих пор мне удалось получить значения объекта «recipe», используя значение для, но мне нужно вручную отобразить {{recipe.description}}.

Однако, как мне сделать то же самое, используявведите [файл] для получения {{recipe.picture}}?

(https://imgur.com/6zfLjBE) enter image description here

Вот мой form.html

<form method="POST" action="{{ url_for('recipes.edit_recipe', recipe_id=recipe.id) }}" enctype="multipart/form-data">
                {{ form.csrf_token }}
                {{ form.hidden_tag() }}
                <fieldset class="form-group">
                    <div class="modal-body">
                    {% for field in form if field.name != "csrf_token" %}
                    {% if field.name != "btn" %}
                            <div class="form-group">
                                {{ field.label(class="form-control-label form-control-lg") }}
                                <!-- if fiend.something == title, need to insert title -->
                                {% if field.name == "picture" %}
                                    {{ field(class="form-control", value=recipe.picture) }}
                                    <figure>
                                        <img src="{{recipe.picture}}" class="small-preview" alt="preview">
                                        <figcaption>Current photo</figcaption>
                                    </figure>
                                {% endif %}
                                {% if field.name == "title" %}
                                    {{ field(class="form-control", value=recipe.title) }}
                                {% endif %}
                                {% if field.name == "description" %}
                                    <textarea class="form-control" name="description" id="description" cols="30" rows="5">{{ recipe.description }}</textarea>
                                {% endif %}
                                <!-- end the if block -->
                                {% for error in field.errors %}
                                    {{ error }}
                                {% endfor %}
                            </div>
                            {% else %}
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                {{ form.btn(class="btn btn-info") }}
                            </div>
                        {% endif %}
                        {% endfor %}
                    </div>
                </fieldset>
            </form>

Вот мой views.py

@recipes_blueprint.route('/<int:recipe_id>', methods=['POST'])
@login_required
def edit_recipe(recipe_id):
    form = UpdateRecipeForm()
    that_recipe = Recipe.get_or_none(Recipe.id == recipe_id)
    recipe_owner = User.get_or_none(User.id == that_recipe.user_id)
    if form.validate_on_submit():
        if recipe_owner != current_user:
            flash("Only owner of the recipe can amend the details", "danger")
            return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
        else:
            if "picture" not in request.files:
                flash("No picture key in request.files", 'danger')
                return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
            file = request.files['picture']
            if file.filename == '':
                flash("Please select a file", 'danger')
                return redirect(url_for('users.update_user'))
            if file and allowed_file(file.filename):
                file.filename = secure_filename(file.filename)
                output = upload_file_to_s3(file, app.config['S3_BUCKET'])
            updated_recipe = Recipe.update(
                title=form.data['title'],
                description=form.data['description'],
                picture=output
            ).where(Recipe.user_id == recipe_owner.id)
            updated_recipe.execute()
            flash("Recipe updated successfully", "success")
            return redirect(url_for('recipes.read_recipe', recipe_id=recipe_id))
    return render_template('read_that_recipe.html', recipe=that_recipe, form=form)
...