Tôi đang phát triển một mặt trước web đơn giản với bình, hiển thị một bảng từ cơ sở dữ liệu và khi người dùng chọn một hàng cụ thể, bình có thông tin của một cột cụ thể của hàng đã chọn.
Với các mã sau, tôi hiển thị một bảng với 5 cột dữ liệu và một cột cuối cùng của nút 'Gửi'. Khi nút 'gửi' của hàng cụ thể, thông tin cột thứ hai ['Tiêu đề'] được cho là sẽ được đăng lại vào ứng dụng Flask.5 columns of data and one last column of 'submit' button. When the 'submit' button of the specific row, the second column ['Title'] information is supposed to be posted back to Flask app.
Nó hoạt động 90%, vì khi nút được nhấp, luôn luôn là thông tin hàng đầu tiên được đăng, ngay cả khi một hàng khác được chọn. Bất cứ ai có thể hình dung những gì đã xảy ra ở đây?first row information is posted, even if a different row is selected. Could anyone figure what went wrong here?
Thanks!
Đây là mã bình
@app.route["/", methods=["GET", "POST"]]
def home[]:
df = pd.read_excel['database.xlsx']
this_list = df.values.tolist[]
if request.method =="POST":
if request.form.get['select_song'] == 'select':
print['selected']
print[request.form.get["title"]]
return render_template['basic_table.html', title='Basic Table',
table=this_list]
Đây là một cơ bản
Type
Title
Location
Translation
Content
selection
{% for row in table %}
{{ row[0] }}
{{ row[1] }}
{{ row[2] }}
{{ row[3] }}
{{ row[4] }}
{% endfor %}
{% endblock %}
Tổng quan
Bình là một khung được sử dụng rộng rãi để tạo API trong Python. Nó là một khung web đơn giản nhưng mạnh mẽ. Flask được thiết kế để giúp chúng tôi làm việc hiệu quả với khả năng mở rộng quy mô đến các ứng dụng phức tạp. is a framework that is widely used to create APIs in Python. It is a simple yet powerful web framework. Flask is designed to help us work efficiently with its ability to scale up to complex applications.
Trong ảnh này, chúng tôi tìm hiểu cách lấy dữ liệu HTML từ ứng dụng bình.
Cài đặt phụ thuộc
Đầu tiên, chúng ta cần chạy các lệnh bên dưới để cài đặt các phụ thuộc cần thiết:
pip install pipenv
pipenv shell
pipenv install Flask
Hãy để tạo một tệp và đặt tên cho nó app.py
.app.py
.
Chúng tôi nhận được HTML input
từ
Type
Title
Location
Translation
Content
selection
{% for row in table %}
{{ row[0] }}
{{ row[1] }}
{{ row[2] }}
{{ row[3] }}
{{ row[4] }}
{% endfor %}
{% endblock %}
0 bằng hàm
Type
Title
Location
Translation
Content
selection
{% for row in table %}
{{ row[0] }}
{{ row[1] }}
{{ row[2] }}
{{ row[3] }}
{{ row[4] }}
{% endfor %}
{% endblock %}
1. Chúng tôi vượt qua tên của đầu vào HTML như một đối số.
Type
Title
Location
Translation
Content
selection
{% for row in table %}
{{ row[0] }}
{{ row[1] }}
{{ row[2] }}
{{ row[3] }}
{{ row[4] }}
{% endfor %}
{% endblock %}
1 function. We pass the name of HTML input as an argument.Hãy nhìn vào một ví dụ:
k38 = request.form.get['kubernetes']
ansible = request.form.get['playbook']
Trong mã trên:
2 lấy đầu vào làm tham số của nó. Trong trường hợp này, đầu vào làType Title Location Translation Content selection {% for row in table %}{{ row[0] }} {{ row[1] }} {{ row[2] }} {{ row[3] }} {{ row[4] }}
3 và được lưu trữ trong biếnType Title Location Translation Content selection {% for row in table %}{{ row[0] }} {{ row[1] }} {{ row[2] }} {{ row[3] }} {{ row[4] }}
4.Type Title Location Translation Content selection {% for row in table %}{{ row[0] }} {{ row[1] }} {{ row[2] }} {{ row[3] }} {{ row[4] }}
5TAKE đầu vào dưới dạng tham số của nó. Trong trường hợp này, đầu vào làType Title Location Translation Content selection {% for row in table %}{{ row[0] }} {{ row[1] }} {{ row[2] }} {{ row[3] }} {{ row[4] }}
6 và được lưu trữ trong biếnType Title Location Translation Content selection {% for row in table %}{{ row[0] }} {{ row[1] }} {{ row[2] }} {{ row[3] }} {{ row[4] }}
7.Type Title Location Translation Content selection {% for row in table %}{{ row[0] }} {{ row[1] }} {{ row[2] }} {{ row[3] }} {{ row[4] }}
Mã ví dụ
from flask import Flask, request, render_template
app = Flask[__name__]
# Creating a route that has both GET and POST request methods
@app.route['/', methods=['GET', 'POST']]
def home[]:
if request.method == 'POST':
name = request.form.get['name']
username = request.form.get['username']
return f'{name}, your username is {username}'
return render_template['home.html']
# Initiating the application
if __name__ == '__main__':
# Running the application and leaving the debug mode ON
app.run[debug=True]
app.py
Giải trình
Chúng tôi tạo một thư mục và đặt tên cho nó
8.Type Title Location Translation Content selection {% for row in table %}{{ row[0] }} {{ row[1] }} {{ row[2] }} {{ row[3] }} {{ row[4] }}
8.Type Title Location Translation Content selection {% for row in table %}{{ row[0] }} {{ row[1] }} {{ row[2] }} {{ row[3] }} {{ row[4] }} Bên trong thư mục
8, chúng tôi tạo một tệp và đặt tên cho nóType Title Location Translation Content selection {% for row in table %}{{ row[0] }} {{ row[1] }} {{ row[2] }} {{ row[3] }} {{ row[4] }}
0. Sau đó, chúng tôi thêm mã sau.pip install pipenv pipenv shell pipenv install Flask
0. Then, we add the following code.pip install pipenv pipenv shell pipenv install Flask
Document
Name:
Username:
Submit
home.html
Trong thiết bị đầu cuối của chúng tôi, chúng tôi chạy lệnh sau để bắt đầu ứng dụng web:
python app.py
click==8.0.3 colorama==0.4.4 Flask==2.0.2 itsdangerous==2.0.1 Jinja2==3.0.3 MarkupSafe==2.0.1 Werkzeug==2.0.2
THẺ LIÊN QUAN
Python
cộng đồng
bình giữ nhiệt
HTML
Người đóng góp