Python 从API调用填充WTForm的推荐方法
我正在将flask与WTforms结合使用,以编辑来自基于python eve的API的数据 我从API中得到了这个对象,请注意“\u id”和“\u etag”:Python 从API调用填充WTForm的推荐方法,python,flask,flask-wtforms,eve,Python,Flask,Flask Wtforms,Eve,我正在将flask与WTforms结合使用,以编辑来自基于python eve的API的数据 我从API中得到了这个对象,请注意“\u id”和“\u etag”: { "_updated": "Sun, 06 Apr 2014 12:49:11 GMT", "name": "moody", "question": "how are you?", "_links": { "self": { "href": "127.0.0.
{
"_updated": "Sun, 06 Apr 2014 12:49:11 GMT",
"name": "moody",
"question": "how are you?",
"_links": {
"self": {
"href": "127.0.0.1:5000/surveys/533e9f88936aa21dd410d4f1",
"title": "Survey"
},
"parent": {
"href": "127.0.0.1:5000",
"title": "home"
},
"collection": {
"href": "127.0.0.1:5000/surveys",
"title": "surveys"
}
},
"_created": "Fri, 04 Apr 2014 12:03:20 GMT",
"_id": "533e9f88936aa21dd410d4f1",
"_etag": "7d11e7f57ed306043d76c05257474670eccde91c"
}
我想在WTForm中编辑对象,因此我创建并定义表单:
class surveyForm(Form):
_id = HiddenField("_id")
_etag = HiddenField("_etag")
name = TextField("Name")
question = TextField("question")
submit = SubmitField("Send")
obj = getSurvey(id) # here I get the object from the API
form = surveyForm(**obj)
return render_template('surveyAdd.html', form=form)
<form class="form-horizontal" role="form" action="{{ url_for('surveyAddOrEdit') }}" method=post>
{{ form.hidden_tag() }}
{{ form._id }}
{{ form._etag }}
{{ form.name.label }}
{{ form.name(class="form-control") }}
{{ form.question.label }}
{{ form.question(class="form-control") }}
{{ form.submit(class="btn btn-default") }}
</form>
启动表格:
class surveyForm(Form):
_id = HiddenField("_id")
_etag = HiddenField("_etag")
name = TextField("Name")
question = TextField("question")
submit = SubmitField("Send")
obj = getSurvey(id) # here I get the object from the API
form = surveyForm(**obj)
return render_template('surveyAdd.html', form=form)
<form class="form-horizontal" role="form" action="{{ url_for('surveyAddOrEdit') }}" method=post>
{{ form.hidden_tag() }}
{{ form._id }}
{{ form._etag }}
{{ form.name.label }}
{{ form.name(class="form-control") }}
{{ form.question.label }}
{{ form.question(class="form-control") }}
{{ form.submit(class="btn btn-default") }}
</form>
并呈现以下表格:
class surveyForm(Form):
_id = HiddenField("_id")
_etag = HiddenField("_etag")
name = TextField("Name")
question = TextField("question")
submit = SubmitField("Send")
obj = getSurvey(id) # here I get the object from the API
form = surveyForm(**obj)
return render_template('surveyAdd.html', form=form)
<form class="form-horizontal" role="form" action="{{ url_for('surveyAddOrEdit') }}" method=post>
{{ form.hidden_tag() }}
{{ form._id }}
{{ form._etag }}
{{ form.name.label }}
{{ form.name(class="form-control") }}
{{ form.question.label }}
{{ form.question(class="form-control") }}
{{ form.submit(class="btn btn-default") }}
</form>
结果是:
<form class="form-horizontal" role="form" action="/survey/add" method="post">
<div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="1396959127.36##dc263965e20ecc9956fc25d5b5c96f90f311cf47"></div>
<UnboundField(HiddenField, ('_id',), {})>
<UnboundField(HiddenField, ('_etag',), {})>
<label for="name">Name</label>
<input class="form-control" id="name" name="name" type="text" value="moody">
<label for="question">question</label>
<input class="form-control" id="question" name="question" type="text" value="how are you?">
<input class="btn btn-default" id="submit" name="submit" type="submit" value="Send">
糟糕…隐藏的“\u id”字段和“\u etag”字段无法渲染。我刚得到一个未绑定的字段
请注意,下划线是条带化的,这就是问题所在
我认为,如果我可以直接从API加载对象并将其加载到表单中,然后在编辑后将其直接反馈到API中,那将是最佳选择……但由于剥离的uderscore,我不能这样做。相反,我改变了对象-将'\u id'重命名为'id',将'\u etag'重命名为etag'-这样就行了
所以我有两个问题:
有没有一种方法可以让物体直接进入形体而不改变它们
与API交互的推荐方式是什么。让客户端方法加载和清理对象是否更好
干杯