简体   繁体   English

如何将数据库 ID 从 flask html 传递到带有 python 的帖子表单中

[英]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.

相关问题 Python / Flask尝试将数据库条目ID从python传递到html - Python/Flask Trying to pass database entry ID from python to html 如何使用flask / jinja将值从python传递到html表单 - How to pass value from python to html form using flask/jinja 如何通过表单将信息从html(jinja)传递到python(flask) - How to pass information from html(jinja) to python(flask) through a form 将信息从 html 表单传递到 python flask - Pass information from html form to python flask 如何使用Python Flask重置HTML表单? - How do I reset my HTML form with Python Flask? 如何从 python flask 中的 RequestIDLogFilter 获取请求 ID? - How do I get the request id from RequestIDLogFilter in python flask? 如何将数据从Python FLASK输入到数据库? - How do I input data from Python FLASK to a database? 如何使用Python发布此HTML表单? - How do I post this HTML form using Python? 如何将存储在变量中的 HTML 表单数据传递给 Flask 中的 Python 脚本? - How to pass HTML form data stored in a variable to a Python script in Flask? 我正在尝试从 html 表单中获取和输入并在 flask 数据库中更新它,最简单的方法是什么? - I am trying to take and input from an html form and have it update in the flask database, whats the simplest way to do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM