我正在尝试将表单条目存储到我的数据库中。我为我的数据库中的每个条目构建了一个RESTful api(我还为数据库构建了一个CRUD api,这使得POST和GET在大多数情况下都非常简单)。我有一个JINJA2 HTML模板,我正在使用Pyramid Web Interface供用户进行交互。我必须承认,我是新手,一直试图跟随金字塔tutorial中的模型而无济于事。
我得到的错误是,我认为,由于路线是以assessment.name
而非id
输入的。 API接受assessment.name和assessment.id。但是api.assessment_retrieve()中的参数触发了错误的东西。由于assessment = api.retrieve_assessment(request.route_url('assessment', id=id))
,我认为这是因为当我在api中放置一个实际的id
时,我得到一个结果,所有的工作原理。
form
页面抓取assessment_id,对吗? 我创建了一个额外的route_uri /done
,用于存储表单提交并将用户保持在同一页面上,而不是202页< - 但在提交后,我仍然得到202(可能是由于HTTPCreate()函数)。
return
将用户路由回模板? 代码: 的 view.py
@view_config(route_name='done', renderer='templates/assessment_form.jinja2')
def form(request):
with transaction.manager:
username = request.params['username']
firstname = request.params['firstname']
lastname = request.params['lastname']
email = request.params['email']
user = api.create_user(username, firstname, lastname, email)
assessment = (request.route_url('assessment', id=id)) # <--- ISSUE
assessment_results = api.create_assessment_results(user, assessment)
transaction.commit()
return HTTPCreated(location=request.route_url('user', id=id)) # <-- redirect so the user keeps working on the form
路线uri (所有实体都遵循用户路由示例)
config.add_route('done', '/done')
config.add_route('users', '/users')
config.add_route('user', '/users/{id:\d+}')
模板
<form method='POST' action='/done'>
<dl class='user_form'>
<dt><label for = 'username'> Username </label></dt>
<dd>{{ forms.input('username') }}</dd>
<dt><label for = 'firstname'> Firstname </label></dt>
<dd>{{ forms.input('firstname') }}</dd>
<dt><label for = 'lastname'> Lastname </label></dt>
<dd>{{ forms.input('lastname') }}</dd>
<dt><label for = 'email'> Email </label></dt>
<dd>{{ forms.input('email') }}</dd>
<dd><input type="submit" name="submit" value="Submit" /></dd>
</dl>
<dl class='category_form'>
<dt><label for = 'category_rating_int'> Situation Awareness </label></dt>
<dd>{{ forms.input('category_rating_int', type=category_rating_int) }}</dd>
<dt><label for = 'category_rating_int'> Decision Making </label></dt>
<dd>{{ forms.input('category_rating_int', type=category_rating_int) }}</dd>
<dt><label for = 'category_rating_int'> Teamwork and Communication </label></dt>
<dd>{{ forms.input('category_rating_int', type=category_rating_int) }}</dd>
<dt><label for = 'category_rating_int'> Leadership </label></dt>
<dd>{{ forms.input('category_rating_int', type=category_rating_int) }}</dd>
<dd><input type="submit" name="submit" value="Submit" /></dd>
</dl>
# more entities
</form>
错误:
2015-09-25 00:35:16,674 INFO [sqlalchemy.engine.base.Engine][Dummy-4] SELECT assessments.assessment_id AS assessments_assessment_id, assessments.name AS assessments_name, assessments.text AS assessments_text, assessments.created_on AS assessments_created_on, assessments.edited AS assessments_edited, assessments.user_id AS assessments_user_id, assessments.video_id AS assessments_video_id
FROM assessments
WHERE assessments.name = ?
2015-09-25 00:35:16,674 INFO [sqlalchemy.engine.base.Engine][Dummy-4] ('http://0.0.0.0:6543/assessments/%3Cbuilt-in%20function%20id%3E',)
2015-09-25 00:35:16,675 INFO [sqlalchemy.engine.base.Engine][Dummy-4] ROLLBACK
2015-09-25 00:35:16,677 ERROR [pyramid_debugtoolbar][Dummy-4] Exception at http://0.0.0.0:6543/done
traceback url: http://0.0.0.0:6543/_debug_toolbar/exception?token=ead4c6c5a3d350eff925&tb=4518219216
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/pyramid_debugtoolbar-2.4.1-py2.7.egg/pyramid_debugtoolbar/toolbar.py", line 192, in toolbar_tween
response = _handler(request)
File "/usr/local/lib/python2.7/site-packages/pyramid_debugtoolbar-2.4.1-py2.7.egg/pyramid_debugtoolbar/panels/performance.py", line 57, in resource_timer_handler
result = handler(request)
File "/usr/local/lib/python2.7/site-packages/pyramid-1.5.7-py2.7.egg/pyramid/tweens.py", line 21, in excview_tween
response = handler(request)
File "/usr/local/lib/python2.7/site-packages/pyramid_tm-0.12-py2.7.egg/pyramid_tm/__init__.py", line 99, in tm_tween
reraise(*exc_info)
File "/usr/local/lib/python2.7/site-packages/pyramid_tm-0.12-py2.7.egg/pyramid_tm/__init__.py", line 80, in tm_tween
response = handler(request)
File "/usr/local/lib/python2.7/site-packages/pyramid-1.5.7-py2.7.egg/pyramid/router.py", line 163, in handle_request
response = view_callable(context, request)
File "/usr/local/lib/python2.7/site-packages/pyramid-1.5.7-py2.7.egg/pyramid/config/views.py", line 355, in rendered_view
result = view(context, request)
File "/usr/local/lib/python2.7/site-packages/pyramid-1.5.7-py2.7.egg/pyramid/config/views.py", line 501, in _requestonly_view
response = view(request)
File "/Users/ack/code/venv/NotssWEB/notssweb/views/views.py", line 32, in form
assessment = api.retrieve_assessment(request.route_url('assessment', id=id))
File "/usr/local/lib/python2.7/site-packages/notssdb/api/object.py", line 115, in retrieve_assessment
filter(Assessment.name == something_unique).one()
File "/usr/local/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2478, in one
raise orm_exc.NoResultFound("No row was found for one()")
NoResultFound: No row was found for one()