Peewee model_form没有更新

时间:2016-12-29 20:01:37

标签: python flask flask-wtforms peewee flask-peewee

我正在使用Peewee的model_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)

1 个答案:

答案 0 :(得分:0)

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

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

尝试:

<form action="{{ url_for('edit_entry') }}" method="POST" class="form">