使用html、python和mysq的下拉菜单创建网页

2024-09-30 00:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经用mysql创建了一个数据库,一个html和python代码,如下所示。如果有人能指出我做错了什么,那真的很有帮助。 html代码不存在加载。需要“从目的地”的下拉列表。但是取而代之的是{{tvalue}} HTML代码如下:(kalyani.html)你知道吗

<head>
    <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>BlueBus</title>
</head>
<body>
<form action="{{ url_for('bluebus') }}" method="POST">
  Date of Travel: <input type="date" name=""><br>
  From Destination: <select name= 'tvalue'>
      {% for tvalue in data%}

            <option value="{{ tvalue }}" selected='selected'>{{ tvalue }}</option>

      {% endfor %}

      </select>
  To Destination: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>
</body>

Python代码如下:

import pymysql
import flask
import simplejson as JSON

def data():  # Execute query
    hostname='127.0.0.1'
    username= 'root'
    password= '******'
    Database= 'mainkar'
    myconnection = pymysql.connect(host=hostname, user=username, password=password, database= Database,autocommit=True)
    cursor=myconnection.cursor()
    sql = 'SELECT distinct(from_city) from route' 
           # Parse in a variable into the query
    cursor.execute(sql)
    list_tested = cursor.fetchall()  # Get query response and store in variable
    list_tested = [i for sub in list_tested for i in sub]  # Convert to list from tuple

    return (list_tested)
x=data()

htmlFilename = 'kalyani.html'
htmlFile = open(htmlFilename, 'w')
app = flask.Flask(__name__)
@app.route('/bluebus',methods=['POST', 'GET'])

def bluebus():
    x=data()
    data = x # you can get list from your DB instead
    data['selected_tvalue'] = tvalue
    return flask.render_template('kalyani.html', **data)

Tags: 代码nameinfromforinputdatahtml
1条回答
网友
1楼 · 发布于 2024-09-30 00:41:17

当函数按以下方式更改时,问题得到解决 &燃气轮机

@app.route('/bluebus',methods=['GET','POST'])
def bluebus():
    x=data()
    for i in range(0,len(x)):
        return flask.render_template('kalyani.html',data=x)
    i=i+1

>

相关问题 更多 >

    热门问题