Issue
I need to send the discussion id to the WTForms form via the Jinja template engine, but I don't understand how to do this
I want to pass the value of a variable, just text with curly brackets is not output.
<div style="background-color:black; border: solid white 1px; ">
<form action="{{ url_for('discussions.creating_discussion') }}" method="POST">
<div>
{{ form.hidden_tag() }}
{{ form.title() }}
</div>
<div style="margin-top: 10px;">
{{ form.hidden_tag() }}
{{ form.text({{ discussion.text }}) }}
</div>
<div>
{{ form.hidden_tag() }}
{{ form.submit() }}
</div>
form
class DiscussionForm(FlaskForm):
title = StringField("Title", validators=[DataRequired()], render_kw={"class": "form-title",
"placeholder": "Введите заголовок"})
text = StringField("Text", validators=[DataRequired()], render_kw={"class": "form-text",
"placeholder": "Ваше сообщение"},
widget=TextArea())
submit = SubmitField("Create", render_kw={"class": "btn btn-success"})
view
@blueprint.route("/community/discussions/<int:discussion_id>/update", methods=["POST", "GET"])
@login_required
def update_discussion(discussion_id):
discussion = Discussions.query.filter(Discussions.id == discussion_id)
if request.method == "POST":
form = DiscussionForm()
if form.validate_on_submit():
discussion.title = form.title.data
discussion.text = form.text.data
try:
db.session.commit()
except:
flash("Произошла ошибка")
return redirect(url_for("community.community_page"))
else:
for field, errors in form.errors.items():
for error in errors:
flash(f"Ошибка в поле: {getattr(form, field).label.text} - {error}")
else:
form = DiscussionForm()
return render_template("discussions.html", discussion=discussion, form=form)
Solution
I'm not sure if I was able to correctly understand from your question what you intended or what your problem was.
To fill the form with the data of your object from the database, you can simply pass this to the obj
attribute of the form within the endpoint. The appropriate attributes of the object are used to fill the form fields of the same name.
@blueprint.route('/community/discussions/<int:discussion_id>/update', methods=['GET', 'POST'])
@login_required
def update_discussion(discussion_id):
discussion = Discussions.query.get_or_404(discussion_id)
form = DiscussionForm(request.form, obj=discussion)
if form.validate_on_submit():
form.populate_obj(discussion)
db.session.commit()
flash("Произошла ошибка")
return redirect(url_for('community.community_page'))
return render_template('discussions.html', **locals())
I also noticed that the URL within the action attribute of the form does not match the route. I think since you're trying to send the form to the same route you get the form from, the action attribute isn't necessary. If you still want to specify it, you can pass the id
of the discussion object as a parameter to url_for
.
By the way, the hidden form fields only need to be integrated once.
<form action="{{ url_for('discussions.update_discussion', discussion_id=discussion.id) }}" method="POST">
{{ form.hidden_tag() }}
<div>
{{ form.title.label() }}
{{ form.title() }}
{% if form.title.errors -%}
<ul>
{% for error in form.title.errors -%}
<li>{{ error }}</li>
{% endfor -%}
</ul>
{% endif -%}
</div>
<div>
{{ form.text.label() }}
{{ form.text() }}
{% if form.text.errors -%}
<ul>
{% for error in form.text.errors -%}
<li>{{ error }}</li>
{% endfor -%}
</ul>
{% endif -%}
</div>
<div>
{{ form.submit() }}
</div>
</form>
Answered By - Detlef
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.