在pycharm用flask显示页面,结果报错
访问首页正常显示helloworld,访问其他页面就报错
from flask import Flask
from flask import request
from flask import render_template
app = Flask(__name__)
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
@app.route('/abc',methods = ['POST'])
def hello_world1(): # put application's code here
return """
<form>
账号:<input><br>
密码:<input>
</form>"""
# @app.route('/tem')
# def hello_world3(): # put application's code here
# return render_template("index.html")
if __name__ == '__main__':
app.run()
网上的方法基本都试过了,重新安装了apache,在.htaccess和httpd.conf中写了相应代码,还是报一样的错
第二个函数的请求方法应该改成GET,停止服务,重新运行,然后访问 http://127.0.0.1:5000/abc 页面。
@app.route('/abc', methods=['GET'])
POST是你提交填入的用户名密码,一般是输入完成后实现一个“提交”按钮,提交按钮上再实现POST的函数。
可以参考:Python Flask实现查询和添加数据
报错的截图,请求的URL是什么?请求方式是什么?
'/abc' 路由返回一个HTML表单,但是表单中的input元素缺少name属性,因此在提交数据时,服务器将不知道如何处理数据。
此外,'/abc' 路由仅支持POST请求,因此在使用GET请求访问该路由时,将出现405错误。
from flask import Flask
from flask import request
from flask import render_template
app = Flask(__name__)
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
@app.route('/abc',methods = ['GET', 'POST'])
def hello_world1(): # put application's code here
if request.method == 'POST':
# Do something with the form data
pass
else:
return """
<form method="post">
账号:<input type="text" name="username">
<br>
密码:<input type="password" name="password">
<br><br>
<input type="submit" value="提交">
</form>"""
# @app.route('/tem')
# def hello_world3(): # put application's code here
# return render_template("index.html")
if __name__ == '__main__':
app.run()
在上面的代码中,我们对路由"/abc"添加了对POST请求的支持,并且在表单中添加了输入框的名称,以便在表单提交后获取其数据。
你按这个代码试试看。有问题再联系我。