Flask - 提交按钮提交所有表单而不是一个表单
Flask - Submit button submits all forms instead of one form
我在我的一个页面中使用了两种表格,一种用于关闭工单,另一种用于发送回复。
但问题是当我提交其中一个时,另一个也被提交了!并显示闪光按摩。所以我有两次快速按摩!
这变得更加复杂,因为我正在检查一些条件以显示第一种形式,在这种情况下,这种形式甚至不再出现,我有两次快速按摩!
@app.route('/edit-ticket', methods=['GET', 'POST'])
def edit_ticket():
if session['logged_in'] == True:
trackingNumberLink = int(request.args.get('trackingNumber'))
closeForm = CloseTicket()
editForm = EditTicket()
GetTicketStatus = tickets.find_one({"trackingNumber": trackingNumberLink})
if closeForm.validate_on_submit():
tickets.update_one({"trackingNumber": trackingNumberLink},
{"$set": {"status": "پاسخ داده شده", "order": 2}})
flash(u"تیکت مورد نظر با موفقیت بسته شد.")
if editForm.validate_on_submit():
replyDate = jdatetime.datetime.now()
tickets.update_one({"trackingNumber": trackingNumberLink},
{"$set": {"status": "در حال بررسی", "order": 1}})
tickets.update_one({"trackingNumber": trackingNumberLink},
{"$push": {"replies": {"rep": {"mass": editForm.ticketMassage.data,
"date": replyDate.strftime("%H:%M:%S %d-%m-%y "),
"HowSent": "user"}}}})
flash(u"پاسخ با موفقیت ارسال شد.")
return render_template('edit-ticket.html', Title="ویرایش تیکت", closeForm=closeForm,
editForm=editForm, CanUserCloseTicket=GetTicketStatus)
else:
return redirect(url_for('Login'))
HTML:
{% extends "layout.html" %}
{% block content_main %}
<div class="container content-box">
<div class="row">
<div class="col-sm-12">
<div class="FormSection center-form">
<fieldset class="form-group">
<legend class="border-bottom mb-4">ویرایش تیکت</legend>
</fieldset>
<form method="post" action="">
{{ editForm.hidden_tag() }}
<div class="form-group">
{{ editForm.ticketMassage.label(class="form-control-label") }}
{% if editForm.ticketMassage.errors %}
{{ editForm.ticketMassage(class="form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in editForm.ticketMassage.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ editForm.ticketMassage(class="form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ editForm.submit(class="btn btn-outline-info") }}
</div>
</form>
</div>
{% if CanUserCloseTicket['status'] != "پاسخ داده شده" %}
<div class="FormSection center-form">
<form method="post" action="">
{{ closeForm.hidden_tag() }}
<div class="form-group">
{{ closeForm.submitCloseTicket(class="btn btn-outline-info") }}
</div>
</form>
</div>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock content_main %}
表格Class:
class EditTicket(FlaskForm):
ticketMassage = TextAreaField('متن پیام:',
description=u'پاسخ خود را بنویسید.',
validators=[data_required(), Length(min=20, max=500)], )
submit = SubmitField('ویرایش تیکت')
class CloseTicket(FlaskForm):
submitCloseTicket = SubmitField('بستن تیکت')
呈现带有 id 属性的表单标签,并为提交和输入标签使用表单属性。
<form id="edit-ticket">
{{ form.submit(form="edit-ticket") }}
The form element that the input element is associated with (its form owner). The value of the attribute must be an id of a element in the same document. If this attribute isn't used, the element is associated with its nearest ancestor element, if any. This attribute lets you to place elements anywhere within a document, not just as descendants of form elements.
更新 submit
使用不同的名称然后在 views.py
if close_form.validate_on_submit() and close_form.close.data:
from flask import Flask, render_template
from flask_wtf import FlaskForm, CSRFProtect
from wtforms.fields import SubmitField, TextAreaField
app = Flask(__name__)
app.config['SECRET_KEY'] = '^%huYtFd90;90jjj'
app.config['UPLOADED_FILES'] = 'static/files'
csrf = CSRFProtect(app)
class EditTicketForm(FlaskForm):
ticket_message = TextAreaField()
edit = SubmitField()
class CloseTicketForm(FlaskForm):
message = TextAreaField()
close = SubmitField()
@app.route('/edit-ticket', methods=['GET', 'POST'])
def edit_ticket():
close_form = CloseTicketForm()
edit_form = EditTicketForm()
if close_form.is_submitted() and close_form.close.data:
if close_form.validate():
x = close_form.message.data
return x.upper()
if edit_form.is_submitted() and edit_form.edit.data:
if edit_form.validate():
y = edit_form.ticket_message.data
return y.upper()
return render_template('edit-ticket.html', close_form=close_form, edit_form=edit_form)
if __name__ == "__main__":
app.run(debug=True)
编辑-ticket.html
<form method="post" id="edit-form" novalidate></form>
<form method="post" id="close-form" novalidate></form>
{{ edit_form.csrf_token(form="edit-form") }}
{{ close_form.csrf_token(form="close-form") }}
{{ edit_form.ticket_message(form="edit-form") }}
{{ edit_form.edit(form="edit-form") }}
{{ close_form.message(form="close-form") }}
{{ close_form.close(form="close-form") }}
在您的表单上执行操作而不是将其留空:
<form method="post" action="/close-ticket"> ... </form>
<form method="post" action="/edit-ticket"> ... </form>
定义显式函数来处理每个动作。一项操作,一项功能 - 保持简单。拆分并重新使用每个登录逻辑。
@app.route('/close-ticket', methods=['POST'])
def close_ticket():
if session['logged_in'] != True:
return redirect(url_for('Login'))
# closeForm handling, etc....
@app.route('/edit-ticket', methods=['POST'])
def edit_ticket():
if session['logged_in'] != True:
return redirect(url_for('Login'))
# editForm handling, etc....
我在我的一个页面中使用了两种表格,一种用于关闭工单,另一种用于发送回复。 但问题是当我提交其中一个时,另一个也被提交了!并显示闪光按摩。所以我有两次快速按摩! 这变得更加复杂,因为我正在检查一些条件以显示第一种形式,在这种情况下,这种形式甚至不再出现,我有两次快速按摩!
@app.route('/edit-ticket', methods=['GET', 'POST'])
def edit_ticket():
if session['logged_in'] == True:
trackingNumberLink = int(request.args.get('trackingNumber'))
closeForm = CloseTicket()
editForm = EditTicket()
GetTicketStatus = tickets.find_one({"trackingNumber": trackingNumberLink})
if closeForm.validate_on_submit():
tickets.update_one({"trackingNumber": trackingNumberLink},
{"$set": {"status": "پاسخ داده شده", "order": 2}})
flash(u"تیکت مورد نظر با موفقیت بسته شد.")
if editForm.validate_on_submit():
replyDate = jdatetime.datetime.now()
tickets.update_one({"trackingNumber": trackingNumberLink},
{"$set": {"status": "در حال بررسی", "order": 1}})
tickets.update_one({"trackingNumber": trackingNumberLink},
{"$push": {"replies": {"rep": {"mass": editForm.ticketMassage.data,
"date": replyDate.strftime("%H:%M:%S %d-%m-%y "),
"HowSent": "user"}}}})
flash(u"پاسخ با موفقیت ارسال شد.")
return render_template('edit-ticket.html', Title="ویرایش تیکت", closeForm=closeForm,
editForm=editForm, CanUserCloseTicket=GetTicketStatus)
else:
return redirect(url_for('Login'))
HTML:
{% extends "layout.html" %}
{% block content_main %}
<div class="container content-box">
<div class="row">
<div class="col-sm-12">
<div class="FormSection center-form">
<fieldset class="form-group">
<legend class="border-bottom mb-4">ویرایش تیکت</legend>
</fieldset>
<form method="post" action="">
{{ editForm.hidden_tag() }}
<div class="form-group">
{{ editForm.ticketMassage.label(class="form-control-label") }}
{% if editForm.ticketMassage.errors %}
{{ editForm.ticketMassage(class="form-control-lg is-invalid") }}
<div class="invalid-feedback">
{% for error in editForm.ticketMassage.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ editForm.ticketMassage(class="form-control-lg") }}
{% endif %}
</div>
<div class="form-group">
{{ editForm.submit(class="btn btn-outline-info") }}
</div>
</form>
</div>
{% if CanUserCloseTicket['status'] != "پاسخ داده شده" %}
<div class="FormSection center-form">
<form method="post" action="">
{{ closeForm.hidden_tag() }}
<div class="form-group">
{{ closeForm.submitCloseTicket(class="btn btn-outline-info") }}
</div>
</form>
</div>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock content_main %}
表格Class:
class EditTicket(FlaskForm):
ticketMassage = TextAreaField('متن پیام:',
description=u'پاسخ خود را بنویسید.',
validators=[data_required(), Length(min=20, max=500)], )
submit = SubmitField('ویرایش تیکت')
class CloseTicket(FlaskForm):
submitCloseTicket = SubmitField('بستن تیکت')
呈现带有 id 属性的表单标签,并为提交和输入标签使用表单属性。
<form id="edit-ticket">
{{ form.submit(form="edit-ticket") }}
The form element that the input element is associated with (its form owner). The value of the attribute must be an id of a element in the same document. If this attribute isn't used, the element is associated with its nearest ancestor element, if any. This attribute lets you to place elements anywhere within a document, not just as descendants of form elements.
更新 submit
使用不同的名称然后在 views.py
if close_form.validate_on_submit() and close_form.close.data:
from flask import Flask, render_template
from flask_wtf import FlaskForm, CSRFProtect
from wtforms.fields import SubmitField, TextAreaField
app = Flask(__name__)
app.config['SECRET_KEY'] = '^%huYtFd90;90jjj'
app.config['UPLOADED_FILES'] = 'static/files'
csrf = CSRFProtect(app)
class EditTicketForm(FlaskForm):
ticket_message = TextAreaField()
edit = SubmitField()
class CloseTicketForm(FlaskForm):
message = TextAreaField()
close = SubmitField()
@app.route('/edit-ticket', methods=['GET', 'POST'])
def edit_ticket():
close_form = CloseTicketForm()
edit_form = EditTicketForm()
if close_form.is_submitted() and close_form.close.data:
if close_form.validate():
x = close_form.message.data
return x.upper()
if edit_form.is_submitted() and edit_form.edit.data:
if edit_form.validate():
y = edit_form.ticket_message.data
return y.upper()
return render_template('edit-ticket.html', close_form=close_form, edit_form=edit_form)
if __name__ == "__main__":
app.run(debug=True)
编辑-ticket.html
<form method="post" id="edit-form" novalidate></form>
<form method="post" id="close-form" novalidate></form>
{{ edit_form.csrf_token(form="edit-form") }}
{{ close_form.csrf_token(form="close-form") }}
{{ edit_form.ticket_message(form="edit-form") }}
{{ edit_form.edit(form="edit-form") }}
{{ close_form.message(form="close-form") }}
{{ close_form.close(form="close-form") }}
在您的表单上执行操作而不是将其留空:
<form method="post" action="/close-ticket"> ... </form>
<form method="post" action="/edit-ticket"> ... </form>
定义显式函数来处理每个动作。一项操作,一项功能 - 保持简单。拆分并重新使用每个登录逻辑。
@app.route('/close-ticket', methods=['POST'])
def close_ticket():
if session['logged_in'] != True:
return redirect(url_for('Login'))
# closeForm handling, etc....
@app.route('/edit-ticket', methods=['POST'])
def edit_ticket():
if session['logged_in'] != True:
return redirect(url_for('Login'))
# editForm handling, etc....