[英]How do I pass the database id from flask html into a post form with python
UPDATE: I think my question is too confusing.更新:我认为我的问题太混乱了。 To sum it up without all of the junk below.总结一下,没有下面的所有垃圾。 How can I pass a parameter in an HTML link into the POST
function of my python file?如何将 HTML 链接中的参数传递到我的 python 文件的POST
function 中? I need that HTML parameter to be part of my commit to the database.我需要将 HTML 参数作为我对数据库的提交的一部分。 My searching so far has not been successful.到目前为止,我的搜索还没有成功。
I have populated an html page event_details.html
with all the table events
entries that have a matching id for table events_id
row.我已经填充了一个 html 页面event_details.html
,其中包含与表events_id
行匹配的所有表events
条目。 In this page, I then have a link button to add another html page add_events_details.html
to add a new entry to the database.然后在这个页面中,我有一个链接按钮来添加另一个 html 页面add_events_details.html
来向数据库添加一个新条目。
This link takes the user to another page with a wtform and I would like to pass the form the same events_id
database row that is being matched to save the user entering this data.此链接将用户带到带有 wtform 的另一个页面,我想将要匹配的相同events_id
数据库行传递给表单,以保存用户输入此数据。
So, the setup is, I pass the event_id of the entry to the add_events_details.html
.因此,设置是,我将条目的 event_id 传递给add_events_details.html
。 But I can only pass this event_id
to the get
and not the post
function, and it is in the post function where I need the event_id
data so I can add it to the database.但我只能将此event_id
传递给get
而不是 function post
,它位于 function 帖子中,我需要event_id
数据,以便将其添加到数据库中。
How can I pass that event_id into my views.py post
function so I can then add it to the database?如何将该 event_id 传递到我的views.py post
function 以便我可以将其添加到数据库中? Thank you in advance.先感谢您。
event_details.html event_details.html
<div class="jumbotron">
{% if current_user.is_authenticated %}
<h1>{{ current_user }}</h1>
<p> This is the event details page </p>
<a href="{{url_for('events.add_event_details', event_id=event_id)}}" class="btn btn-outline-primary" role="button">Add Schedule</a>
</div>
....more code... to show entries
add_event_details.html add_event_details.html
<div class="jumbotron">
{% if current_user.is_authenticated %}
<h1>{{ current_user }}</h1>
<p> This is the event details page </p>
<a href="{{url_for('events.add_event_details', event_id=event_id)}}" class="btn btn-outline-primary" role="button">Add Schedule</a>
</div>
And the views.py is views.py 是
class AddEventDetailsView(MethodView):
decorators = [login_required]
template_file = 'add_event_details.html'
form_class = EventForm
def get(self, event_id):
return render_template(self.template_file, form=self.form_class())
def post(self):
form = self.form_class()
print(form.errors)
if form.validate_on_submit():
# I added this print to find out its type
print(type(form.events_id.data))
# This form returns the class model, so I just have to ask it for the \
# specific column that I want it to return
# events_id = form.events_id.data.id
user_id = current_user.id # This assumes that the person logged in is the company
eventdetails = Eventdetails(
schedule_name=form.schedule_name.data,
event_id=event_id,
user_id=user_id,
naics=form.naics.data
)
print(f"The user id is {user_id}")
db.session.add(eventdetails)
db.session.commit()
flash("Thank you for registering your event.")
return redirect(url_for('events.event_details', event_id=event_id))
I think I figured this out.我想我想通了。 I can just pass the same parameter into my POST
request and use it that way and it works fine.我可以将相同的参数传递到我的POST
请求中并以这种方式使用它,它工作正常。
So, instead of所以,而不是
def get(self, event_id):
I just can do我只能做
def post(self, event_id):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.