【问题标题】:creating a web page with dropdown with html ,python and mysql使用 html、python 和 mysql 创建一个带有下拉菜单的网页
【发布时间】:2020-01-07 15:06:02
【问题描述】:

我在 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)

【问题讨论】:

标签: python html mysql flask


【解决方案1】:

通过以下方式更改功能时问题已解决 >

@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

>

【讨论】:

    猜你喜欢
    • 2017-04-04
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多