note:这两天在学习Python Flask Tutorials,其中Connecting a database运行示例程序的时候出现一个问题,折腾了我2天时间,各种搜索网上的资料,今天下午终于解决了,现贴出来供大家参考,以免浪费时间。
python版本:python3.8.1
python运行环境:win10 + visual studio code 1.42.1
python学习链接:https://pythonspot.com/python-flask-tutorials/
示例程序链接:https://pythonspot.com/login-authentication-with-flask/
Login authentication with Flask
1、Create the filetabledef.py:
创建tabledef.py文件后,from sqlalchemy import create_engine会提示sqlalchemy未导入进来(import error),解决方法如下:
1、点右下角设置,然后点setting, 在打开的页面点右上角的小按钮,打开setting.json文件,并在文件中添加以下代码即可:
{
"python.pythonPath": "C:\\Users\\cumin\\AppData\\Local\\Programs\\Python\\Python38\\python.exe",
}
2、Create a file calleddummy.py
创建dummy.py后,运行提示:__init__() takes 1 positional argument but 3 were given
这个问题困扰了我足足两天,网上有的人说可能是参数问题,大多数认为是类的引用参数对应不上:
经过反复加减参数都不行,最后突然发现User类下面的方法__init__()没有缩进,所以类没法引用,天了撸,这是官网直接拷贝代码的结果啊,所以建议大家在跑示例程序的时候,务必要注意代码缩进问题,修改之后就OK了,如下:
3、Finally we updateourapp.py
关于这个代码,直接copy代码运行会提示:
File "D:\Linux\app.py", line 15, in home
return"Hello Boss! Logout < /a> "/logout #" > Logout < /a > "
TypeError: unsupported operand type(s) for/: 'str' and 'function'
解决方法是,把内部双引号改为单引号,如下:
最后,运行并在按Ctrl+鼠标左键,将自动打开默认浏览器网页,如下:
在登录框内输入以下任一用户名和密码,都可以登录成功,如下所示:
User ("admin" , "password")
User ("python" , "python")
User ("jumpiness" , "python")