【发布时间】:2019-08-26 14:52:42
【问题描述】:
我想使用 python、Flask 和 mongoDB 执行 INSERTION 操作。 当我在服务器上运行我的代码时,它显示“在服务器上找不到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试”。我的代码有什么问题。请提前帮助和谢谢。
from flask import Flask,render_template,request
import pymongo
app=Flask(__name__)
app.secret_key = 'development key'
@app.route('/insert',methods=['POST','GET'])
def enter():
myclient=pymongo.MongoClient('mongodb://localhost:27017/')
mydb=myclient['student']
mycol=mydb['knit']
if request.method=='POST':
query={'name':request.form['name'],'age':request.form['age'],'city':request.form['city'],'company':request.form['company']}
x=mycol.insert_one(query)
print(x)
if __name__=='__main__':
app.run(debug=True)
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form method="post" action="/insert" required>
Username:<input type="text" name="name" required><br>
Age:<input type="text" name="age" required><br>
City:<input type="text" name="city" required><br>
Comapny:<input type="text" name="comapny" required><br>
<input type="submit" name="submit">
</form>
</body>
</html>
【问题讨论】:
-
默认使用 IP
127.0.0.1,这意味着它不能从其他计算机访问。您可能必须使用app.run(host='0.0.0.0', debug=True) -
在“0.0.0.0”处显示 =====“无法访问此站点0.0.0.0 处的网页可能暂时关闭,或者它可能已永久移动到新网址.ERR_ADDRESS_INVALID"
-
我解决了问题。谢谢先生。弗拉斯