3
我嘗試使用Flask創建一個基本的會計包,我有點卡在驗證特定的表單。它正在爲同一個應用程序中的另一個表單工作,但出於某種原因,我無法弄清楚爲什麼這個工具不能正常工作。表單顯示爲它應該在網絡瀏覽器中,我可以正常輸入數據,但是當我提交表單時,它似乎只是重新加載頁面。我的數據庫沒有得到更新,經過一些挖掘,我發現我的if form.validate_on_submit()
沒有捕捉任何東西。Flask WTForms表單沒有提交,但輸出沒有驗證錯誤
堆棧跟蹤中也沒有發生任何錯誤。
我將在下面發佈我的文件的相關部分。
我views.py
:
@app.route('/dashboard/<nickname>/work-orders/add/', methods = ['GET', 'POST'])
@login_required
def work_orders_add(nickname):
ca = check_active(nickname)
if ca[1] == True:
form = AddWorkOrderForm()
u = User.query.get(current_user.id)
c = u.contacts.order_by('name').all()
form.requestor.choices = [(contact, contact.name) for contact in c]
if form.validate_on_submit():
db.session.add(WorkOrder(requestor = form.requestor.data,
date = form.date.data,
notes = form.notes.data,
total = 0.00,
completed = form.completed.data,
user = g.user))
db.session.commit()
return redirect(url_for('work_orders', nickname=current_user.nickname))
elif request.method != "POST":
form.date.data = datetime.date.today()
else: return "didn't work"
return render_template('work_orders_add.html',
user = ca[0],
title = "New Work Order",
form = form)
else: return redirect(url_for('work_orders', nickname=current_user.nickname))
我forms.py
:
class AddWorkOrderForm(Form)
requestor = SelectField('requestor', coerce=unicode)
date = DateField('date')
notes = TextAreaField('notes')
completed = BooleanField('completed', default = False)
我models.py
:
class WorkOrder(db.Model):
__tablename__ = 'workorder'
id = db.Column(db.Integer, primary_key = True)
date = db.Column(db.String(64))
notes = db.Column(db.String(1024))
total = db.Column(db.Float)
completed = db.Column(db.Boolean)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
contact_id = db.Column(db.Integer, db.ForeignKey('contact.id'))
items = db.relationship('WorkOrderItem', primaryjoin="WorkOrder.id==WorkOrderItem.work_order_id", backref = 'order')
def __repr__(self):
return '<WorkOrder %r>' % (self.id)
我的HTML文件:
{% extends "_is_logged.html" %}
{% block content %}
<h1>{{ title }}</h1>
<form action="" method="post" name="work_order">
{{ form.hidden_tag() }}
<p>Requestor: {{ form.requestor() }}</p>
<p>Date: {{ form.date() }}</p>
<p>Notes: {{ form.notes(size=70) }}</p>
<p>Completed: {{ form.completed() }}</p>
<p><input type="submit" value="Add Work Order"></p>
</form>
{% endblock %}
謝謝。
會發生什麼事,如果你添加一個'{%FIELD_NAME的,在form.errors.items field_errors()%}
{{FIELD_NAME}}
{%error for field_errors%}- {{error}}
{%endfor%}
{%endfor%}'您的模板文件? –我曾嘗試添加,但似乎沒有改變。您的代碼是否需要更改,但放入我的模板文件中?或者應該簡單的複製和粘貼工作? – LJPPython
不,它應該剛剛工作,只要你把它放在'內容'塊... –