0

我正在使用 Peeweemodel_form更新数据库中条目的实例。以下是我的表单、模板和应用程序代码。出于某种原因,我认为我在POST提交表单时没有收到,但我正在以与过去相同的方式编写代码,没有任何问题。任何帮助,将不胜感激!

布赖恩

forms.py

from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, DateField
from wtforms.validators import DataRequired
from wtfpeewee.orm import model_form

import models


class EntryForm(FlaskForm):
    """Form for adding a journal entry to the database."""
    title = StringField(validators=[DataRequired()])
    date = DateField(validators=[DataRequired()])
    time_spent = StringField(validators=[DataRequired()])
    learning = TextAreaField(validators=[DataRequired()])
    resources = TextAreaField(validators=[DataRequired()])


EditEntryForm = model_form(models.Entry)

edit.html

{% extends 'layout.html' %}
{% from 'macros.html' import render_field, field_error %}

{% block content %}
<div class="edit-entry">
    <h2>Edit Entry</h2>
    <form>
        {# Form to edit journal entry #}
    <form action="" method="POST" class="form">
        {# {{ form.hidden_tag() }} #}
        <label for="title">Title</label>
        {{ render_field(form.title) }}
        <label for="date">Date</label>
        {{ field_error(form.date) }}
        <input id="date" type="date" name="date" value="{{ entry.date.strftime('%Y-%m-%d') }}"><br>
        <label for="time-spent">Time Spent</label>
        {{ render_field(form.time_spent) }}
        <label for="what-i-learned">What I Learned</label>
        {{ render_field(form.learning) }}
        <label for="resources-to-remember">Resources to Remember</label>
        {{ render_field(form.resources) }}

        <input type="submit" value="Update Entry" class="button">
        <a href="{{ url_for('details', entry_id=entry.id) }}" class="button button-secondary">Cancel</a>
    </form>
</div>
{% endblock %}

app.py

@app.route("/edit/<int:entry_id>", methods=['POST', 'GET'])
def edit_entry(entry_id):
    """
    Allows user to edit a journal entry with the following fields: Title, Date,
    Time Spent, What You Learned, Resources to Remember.
    """
    try:
        entry = models.Entry.select().where(models.Entry.id==entry_id).get()
    except models.Entry.DoesNotExist:
        abort(404)
    else:
        if request.method == 'POST':
            form = forms.EditEntryForm(request.form, obj=entry)
            if form.validate():
                form.populate_obj(entry)
                entry.save()
                flash("Journal entry has been updated!", "success")
                return redirect(url_for('details', entry_id=entry.id))
        else:
            form = forms.EditEntryForm(obj=entry)

    return render_template("edit.html", form=form, entry=entry)
4

1 回答 1

0

您似乎忘记向表单添加操作:

<form action="" method="POST" class="form">

尝试:

<form action="{{ url_for('edit_entry') }}" method="POST" class="form">
于 2017-02-06T03:40:28.907 回答