[英]How to POST to non def post(self) functions in Flask-Classy?
Merry Christmas. 圣诞节快乐。
I am not sure if this is possible. 我不确定这是否可能。 If I have a view-index and I have 2 forms on the view's template, how can I have them direct to different function?
如果我有一个视图索引,并且该视图的模板上有2个表单,如何将它们直接用于其他功能?
My view code (as a simple example) is as follows: 我的视图代码(作为简单示例)如下:
def deletex(input):
…
class AdminView(FlaskView):
@route('/')
def index(self):
return render_template(‘admin-home.html’)
def delete_x(self, methods=['POST']):
x = request.form['gid']
deletex(x)
flash(“All’s good")
return redirect(url_for(index))
Quite a simple example. 很简单的例子。 The form on admin-home.html includes this…
admin-home.html上的表格包括此…
<form action=“{{ url_for(‘AdminView:delete_x’) }}” method=“post”>
...
When I do that it returns a 405 Error. 当我这样做时,它将返回405错误。 I can submit forms via GET to delete_x with GET allowed as a method but I can't find a way to post to a flask-classy view that is not the
def post(self): …
我可以通过GET将表单提交到delete_x,并允许使用GET作为方法,但是我找不到方法发布到不是
def post(self): …
的flask-classy视图def post(self): …
How can I do this? 我怎样才能做到这一点? Is it possible?
可能吗? I'd rather not have 2 forms worth of logic in one view.
我宁愿在一种视图中没有2种形式的逻辑价值。
The error was because I was putting the methods=['POST']
in the wrong spot. 错误是因为我将
methods=['POST']
放在错误的位置。 I had experimented with it in the function declaration rather than the route decorator. 我已经在函数声明而不是路由装饰器中进行了实验。
My class defn is: 我的课堂defn是:
@route('deletex', methods=['POST'])
def deletex(self):
pass
And then my HTML form is: 然后我的HTML表单是:
<form action="{{ url_for("AdminView:deletex") }}" method="post">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.