写在前面:突然有一个任务,需要利用bottle框架开发一个小网站程序,第一次接触到MVC的模型,开发过程中最大的一个感受就是力不从心,需要的编程语言知识很缺乏,特别是前端的知识,但最大的收获是能够自己去阅读文档,自己去解决问题,一步一步实现自己想要实现的功能
bottle框架的官方文档
W3Cschool各种官方文档
1、set_cookie() missing 1 required positional argument: ‘self’
- 出错的原因:
from bottle import BaseResponse
BaseResponse.set_cookie()#出错在这里,BaseResponse是一个类
- 解决办法
第一种方法,实例化一个对象
from bottle import BaseResponse
response = BaseResponse()
response.set_cookie(参数列表)
第二种方法,用request
和response
from bottle import request response
response.set_cookie(参数列表)
2、sqlite3.OperationalError: no such column: Alice
- 出错语句
sql_query = """
select * from messages
where userTo = {cur_user}
"""
原因是因为:sql语句中占位符没有加引号
- 解决办法
sql_query = """
select * from messages
where userTo = '{cur_user}'
"""
3、AttributeError: ‘function’ object has no attribute ‘View’
- 可能出错地方
from bottle import *
page_view = view.View()
#这两个python语句对于view有冲突,bottle里面有view函数
- 解决办法
from bottle import request response
#准确引入包,或者改view.py这个文件名字
4、pycharm 中通过ctrl进入函数,如何返回到原函数
View --> Appearance --> Toolbar ->回退功能
5、How to make bottle server HTTPS python
参考链接
- 解决方法
可以利用Gunicorn
这个库
#安装 pip install Gunicorn
import bottle
from bottle import Bottle
BASE = Bottle()
@BASE.route('/', ['GET'])
def index():
return 'Index'
bottle.run(
app=BASE,
host='0.0.0.0',
port='8888',
server='gunicorn',
reloader=1,
debug=1,
keyfile='key.pem',
certfile='cert.pem'
)