gpt4 book ai didi

python - Flask - WTForm - 将表单保存到数据库

转载 作者:行者123 更新时间:2023-11-28 19:56:11 26 4
gpt4 key购买 nike

我在将表单 (wtf) 保存到 (sqlalchemy) db 时遇到问题,表单正在呈现,但提交后没有任何反应......

例如:

127.0.0.1 - - [30/Dec/2013 10:30:24] "POST /add/ HTTP/1.1" 200 -

我试图在没有验证器的情况下保存

例如:

name = TextField('Task Name')

我试图以其他方式保存表格:

if request.method == 'POST' and form.validate():
new_task = Tasks(
form.name.data,
form.due_date.data,
form.priority.data,
form.posted_date.data,
session['user_id'],
form.category.data,
form.super_category.data,
form.description.data
)
db.session.add(new_task)
db.session.commit()

下面的代码:

模型:

# -*- coding: utf-8; -*-

from app import db
import datetime


class Tasks(db.Model):

__tablename__ = "tasks"

task_id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(255),nullable=False)
description = db.Column(db.Text,nullable=False)
priority = db.Column(db.Integer,nullable=False)
posted_date = db.Column(db.DATE,nullable=False)
status = db.Column(db.Integer,default=1,nullable=False)
category = db.Column(db.String(255),nullable=False)
super_category = db.Column(db.String(255),nullable=False)
user_id = db.Column(db.Integer,db.ForeignKey('users.id'))

def __init__(self,name,due_date,priority,posted_date,users_id,category,super_category,description):
self.name = name
self.due_date = due_date
self.priority = priority
self.posted_date = posted_date
self.user_id = users_id
self.category = category
self.super_category = super_category
self.description = description

表格:

 # -*- coding: utf-8; -*-
from wtforms import Form,validators
from wtforms import TextField,DateField,IntegerField,SelectField,TextAreaField
from wtforms.validators import required,Email,EqualTo,Length
from wtforms import PasswordField

class AddTask(Form):
name = TextField('Task Name',validators=[required()])
due_date = DateField('Date Due (mm/dd/yyyy)',validators=[required()],format='%m/%d/%Y')
priority = SelectField('Priority',validators=[required()],choices=[('1','1'),('2','2'),('3','3'),('4','4'),('5','5')])
description = TextAreaField('Description',validators=[required()])
posted_date = DateField('Posted Date (mm/dd/yyyy)',validators=[required()],format='%m/%d/%Y')
category = SelectField('Category',validators=[required()],choices=[('foobar','foobar'),('foobar2','foobar2')])
super_category = SelectField('Super_category',validators=[required()],choices=[('foobar3','foobar3'),('foobar4','foobar4'),('foobar5','foobar5')])
user_id = SelectField('User',validators=[required()],choices=[('1','1'),('2','2'),('3','3'),('4','4'),('5','5')])

观看次数:

@app.route('/add/',methods=['GET','POST'])
@login_required
def new_task():
form = AddTask(request.form,csrf_enabled=True)
if request == 'POST' and form.validate():
form_tasks = Tasks()
form.populate_obj(form_tasks)
db.session.add(form_tasks)
db.session.commit()
return redirect(url_for('tasks'))
return render_template('form.html',form=form)

表单.html :

{% extends "layout2.html" %}
{% from 'common.html' import edit_field %}
{% block content %}
<div class='edit well offset 2 span8'>
<form method='post' class="form-horizontal">
<legend> Add task </legend>
{{ edit_field(form.name , class="span3") }}
{{ edit_field(form.due_date , class="span3", type="datetime") }}
{{ edit_field(form.priority, class="span3") }}
{{ edit_field(form.description, rows="5" ,class="span3", placeholder="foobar" ) }}
{{ edit_field(form.posted_date, class="span3",type="datetime") }}
{{ edit_field(form.category, class="span3") }}
{{ edit_field(form.super_category, class="span3") }}
<div class="form-actions">
<button type="submit" class="btn">SAVE</button>
</div>
</form>
</div>
{% endblock %}

通用.html:

{% macro edit_field(field,catch_kwargs=true) %}
<div class="control-group{% if field.errors %} error {% endif %}">
{{ field.label(class="control-label") }}
<div class="controls">
{{ field(**kwargs) }}
{% for error in field.errors %}
<span class="help-inline">{{error}}</span>
{% endfor %}
</div>
</div>
{% endmacro %}

最佳答案

您需要在表单中的某处放置防伪标记。在模板文件中的 form 标记内添加以下内容:

{{ form.hidden_tag() }}

这呈现类似于

的隐藏字段
<input id="csrf_token" name="csrf_token" type="hidden" value="xxxxxx">

将它与我们得到的 form.html 文件放在一起:

{% extends "layout2.html" %}
{% from 'common.html' import edit_field %}
{% block content %}
<div class='edit well offset 2 span8'>
<form method='post' class="form-horizontal">
{{ form.hidden_tag() }}
<legend>Add task</legend>
{{ edit_field(form.name , class="span3") }}
{{ edit_field(form.due_date , class="span3", type="datetime") }}
{{ edit_field(form.priority, class="span3") }}
{{ edit_field(form.description, rows="5" ,class="span3"
, placeholder="foobar" ) }}
{{ edit_field(form.posted_date, class="span3",type="datetime") }}
{{ edit_field(form.category, class="span3") }}
{{ edit_field(form.super_category, class="span3") }}
<div class="form-actions">
<button type="submit" class="btn">SAVE</button>
</div>
</form>
</div>
{% endblock %}

关于python - Flask - WTForm - 将表单保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20837209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com