第一页flask:
import math
from flask import Flask,abort,render_template,session,request
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:123456@localhost:3306/woniunote?charset=utf8'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
db = SQLAlchemy(app)
@app.route('/search/<kw1>')
def home(kw1):
from module.article import Article
a1=Article()
result=a1.find_by_headline(kw1)
print(result)
return result
@app.route('/')
def form():
from module.article import Article
a1 = Article()
s=a1.cha()
return s
# return render_template('1.html')
@app.route('/submit',methods=['POST'])
def submit():
kw1=request.form.get('kw')
from module.article import Article
a1=Article()
result=a1.find_by_headline(kw1)
print(result)
return result
@app.route('/upload',methods=['POST'])
def upload():
file=request.files.get('file')
if file:
filename=file.filename
file.save(f'E:/{filename}')
return f'File uploaded successfully:{filename}'
return 'NO file upload'
if __name__=='__main__':
app.run(debug=True)
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/4b3f7daa845749439e127fe4d723fb4e.png "#left")
第二页代码
import time
from flask import session
from sqlalchemy import Table
from main1 import db
class Article(db.Model):
__table__=Table('article',db.metadata,autoload_with=db.engine,mysql_bind=db.engine)
def model_list(result):
list=[]
for row in result:
dict={}
for k,v in row.__dict__.items():
if not k.startswith('_sa_instance_state'):
dict[k]=v
list.append(dict)
print('后:',list)
return list
def find_by_headline(self,kw1):
result1=db.session.query(Article).filter(
Article.headline.like('%'+kw1+'%')).all()
print('qian',result1)
for i in result1:
i.content='内容test'
result=Article.model_list(result1)
return result
def cha(self):
a1=db.session.query(Article).all()
print(a1)
请问一下为什么会一直报这个错误,另一章是网页截图,我需要实现flask能有一个搜索框搜索内容并且能访问到Mysql里面,如果各位有更好实现的方法希望分享一下,感激不尽