我有一个删除路线,我想测试它但是得到了这个错误,我认为它的路径问题我会非常感激,如果你可以帮助我 所以当我删除用户时,它将指向页面“// 127.0.0.1:5000/user/1?sortby=normal&id=1”并说“删除已完成!” 我真的需要帮助我也可以在删除路由中注释@login_required但是仍然无法正常工作
ps:布局是蓝图
这是错误:
Traceback (most recent call last):
File "/home/peg/flask-Alembic/test/test_app.py", line 47, in test_user_delete
response = self.client.get(url_for('/user/{0}?sort=normal&monkey='.format(u1.id),follow_redirects=True))
File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 312, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/helpers.py", line 305, in url_for
force_external=external)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/routing.py", line 1616, in build
raise BuildError(endpoint, values, method)
BuildError: ('layout.user/1?sort=normal&monkey=&id=1', {'follow_redirects': True}, None)
test_app.py:
def test_user_delete(self):
e1 = Users(name='admine2', email='admine2@gmail.com', age=25)
db.session.add(e1)
db.session.commit()
u1= Users.query.get(e1.id)
print('{0}').format(u1.id)
response = self.client.get(url_for('layout.user/{0}?sort=normal&monkey='.format(u1.id),follow_redirects=True))
self.assertEqual('delete done !.', response.data)
route.py:
@layout.route('/delete/<int:id>')
@login_required
def delete(id):
"""remove monkey"""
user = Users.query.get(id)
if g.user.id == user.id:
flash('You are not allow to delete yourself.')
else:
db.session.delete(user)
db.session.commit()
flash('delete done !.')
return redirect(url_for('layout.user',id=id, page=1,sortby='normal'))
答案 0 :(得分:0)
由于您致电
,您收到错误消息response = self.client.get(url_for('layout.user/{0}?sort=normal&monkey='.format(u1.id),follow_redirects=True))
修改强>
您应该将该通话更改为
response = self.client.get(url_for('layout.delete', id=u1.id), follow_redirects=True)
请参阅http://flask.pocoo.org/docs/0.10/blueprints/#building-urls了解如何使用蓝图和url_for