Skip to content

Commit 738ee50

Browse files
authored
Upload folder
1 parent 505e7ea commit 738ee50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1103
-0
lines changed
429 Bytes
Binary file not shown.

db_demo1/config.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# dialect+driver://username:password@host:port/database
2+
3+
DIALECT = 'mysql'
4+
DRIVER = 'pymysql'
5+
USERNAME = 'root'
6+
PASSWORD = 'init#201605'
7+
HOST = '127.0.0.1'
8+
PORT = '3306'
9+
DATABASE = 'db_demo2'
10+
11+
SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)
12+
SQLALCHEMY_TRACK_MODIFICATIONS = False

db_demo1/db_demo1.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
import config
4+
5+
6+
app = Flask(__name__)
7+
app.config.from_object(config)
8+
db = SQLAlchemy(app)
9+
10+
#articletable:
11+
#create table articl(
12+
# id int primary key autoincrement,
13+
# title varchar(100) not null,
14+
# content text not null,
15+
#)
16+
17+
class Article(db.Model):
18+
__tablename__ = 'article'
19+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
20+
title = db.Column(db.String(100), nullable=False)
21+
content = db.Column(db.Text, nullable=False)
22+
23+
db.create_all()
24+
25+
@app.route('/')
26+
def hello_world():
27+
# article1 = Article.query.filter(Article.content == 'bbb').first()
28+
# db.session.delete(article1)
29+
# db.session.commit()
30+
31+
32+
return 'Hello World!'
33+
34+
35+
if __name__ == '__main__':
36+
app.run(debug= True)
429 Bytes
Binary file not shown.

db_demo3/config.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# dialect+driver://username:password@host:port/database
2+
3+
DIALECT = 'mysql'
4+
DRIVER = 'pymysql'
5+
USERNAME = 'root'
6+
PASSWORD = 'init#201605'
7+
HOST = '127.0.0.1'
8+
PORT = '3306'
9+
DATABASE = 'db_demo3'
10+
11+
SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)
12+
SQLALCHEMY_TRACK_MODIFICATIONS = False

db_demo3/db_demo3.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
import config
4+
5+
app = Flask(__name__)
6+
app.config.from_object(config)
7+
db = SQLAlchemy(app)
8+
9+
class Users(db.Model):
10+
__tablename__ = 'users'
11+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
12+
username = db.Column(db.String(100), nullable=True)
13+
14+
class Article(db.Model):
15+
__tablename__ = 'article'
16+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
17+
title = db.Column(db.String(100), nullable=True)
18+
content = db.Column(db.Text, nullable=True)
19+
author_id = db.Column(db.Integer,db.ForeignKey('users.id'))
20+
author = db.relationship('Users', backref=db.backref('articles'))
21+
22+
db.create_all()
23+
24+
@app.route('/')
25+
def index():
26+
# 先添加一个新user用来添加新文章
27+
# user1 = Users(username = 'Zac')
28+
# db.session.add(user1)
29+
# db.session.commit()
30+
31+
# 再添加一片新文章
32+
# article1 = Article(title = 'title1', content = 'content1', author_id = 1)
33+
# db.session.add(article1)
34+
# db.session.commit()
35+
36+
# 找标题为aaa的作者
37+
# 复杂方法:
38+
# article = Article.query.filter(Article.title == 'title1').first()
39+
# author_id = article.author_id
40+
# user = Users.query.filter(Users.id == author_id).first()
41+
# print(user.username)
42+
# 简单方法:
43+
# 在Article类中加入author = db.relateionship('Users', backref=db.backref('articles'))
44+
# article = Article(title = 'title1', content='content1')
45+
# article.author = Users.query.filter(Users.id == 1).first()
46+
# db.session.add(article)
47+
# db.session.commit()
48+
article = Article.query.filter(Article.title == 'title1').first()
49+
print(article.author.username)
50+
return 'index'
51+
52+
if __name__ == '__main__':
53+
app.run(debug=True)

db_demo3/manage.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from flask_script import Manager
429 Bytes
Binary file not shown.

db_demo4/config.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# dialect+driver://username:password@host:port/database
2+
3+
DIALECT = 'mysql'
4+
DRIVER = 'pymysql'
5+
USERNAME = 'root'
6+
PASSWORD = 'init#201605'
7+
HOST = '127.0.0.1'
8+
PORT = '3306'
9+
DATABASE = 'db_demo4'
10+
11+
SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT,DRIVER,USERNAME,PASSWORD,HOST,PORT,DATABASE)
12+
SQLALCHEMY_TRACK_MODIFICATIONS = False

db_demo4/db_demo4.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from flask import Flask
2+
from flask_sqlalchemy import SQLAlchemy
3+
import config
4+
5+
app = Flask(__name__)
6+
app.config.from_object(config)
7+
db = SQLAlchemy(app)
8+
9+
10+
article_tag = db.Table('article_tag',
11+
db.Column('article_id', db.Integer, db.ForeignKey('article.id'), primary_key=True),
12+
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True),
13+
)
14+
15+
class Article(db.Model):
16+
__tablename__ = 'article'
17+
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
18+
title = db.Column(db.String(100),nullable=True)
19+
tags = db.relationship('Tag', secondary=article_tag, backref=db.backref('articles'))
20+
21+
class Tag(db.Model):
22+
__tablename__ = 'tag'
23+
id = db.Column(db.Integer, primary_key=True , autoincrement=True)
24+
name = db.Column(db.String(50), nullable=True)
25+
26+
27+
28+
29+
db.create_all()
30+
31+
@app.route('/')
32+
def hello_world():
33+
34+
# article1 = Article(title = 'aaa')
35+
# article2 = Article(title = 'bbb')
36+
#
37+
# tag1 = Tag(name='111')
38+
# tag2 = Tag(name='222')
39+
#
40+
# article1.tags.append(tag1)
41+
# article1.tags.append(tag2)
42+
#
43+
# article2.tags.append(tag1)
44+
# article2.tags.append(tag2)
45+
#
46+
#
47+
# db.session.add(article1)
48+
# db.session.add(article2)
49+
# db.session.add(tag1)
50+
# db.session.add(tag2)
51+
# db.session.commit()
52+
53+
article1 = Article.query.filter(Article.title=='aaa').first()
54+
tags = article1.tags
55+
for tag in tags:
56+
print(tag.name)
57+
58+
return 'Hello_World'
59+
60+
61+
if __name__ == '__main__':
62+
app.run(debug=True)

db_demo4/manage.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from flask_script import Manager

extends_block.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# encoding: utf-8
2+
from flask import Flask, render_template
3+
4+
app = Flask(__name__)
5+
6+
7+
8+
@app.route('/')
9+
def index():
10+
return render_template('index.html')
11+
12+
if __name__ == '__main__':
13+
app.run(debug=True)

filter_demo.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# encoding: utf-8
2+
from flask import Flask, render_template
3+
4+
app = Flask(__name__)
5+
6+
@app.route('/')
7+
def index():
8+
comments = [
9+
{
10+
'user': 'Zac',
11+
'content': '119'
12+
},
13+
{
14+
'user': 'Cecelia',
15+
'content': '829'
16+
}
17+
]
18+
return render_template('index.html', comments=comments)
19+
20+
21+
if __name__ == '__main__':
22+
app.run(debug=True)

for_statement.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# encoding: utf-8
2+
from flask import Flask, render_template
3+
4+
app = Flask(__name__)
5+
6+
7+
#for遍历字典
8+
9+
@app.route('/')
10+
def index():
11+
user = {
12+
'username': 'Zac',
13+
'age': 20
14+
}
15+
return render_template('index.html', user = user)
16+
17+
if __name__ == '__main__':
18+
app.run(debug=True)
329 Bytes
Binary file not shown.

g_demo/g_demo.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from flask import Flask, g, render_template, request
2+
from utils import login_log
3+
app = Flask(__name__)
4+
5+
6+
@app.route('/')
7+
def hello_world():
8+
return 'Hello World!'
9+
10+
@app.route('/login/', methods=['GET', 'POST'])
11+
def login():
12+
if request.method == 'GET':
13+
return render_template('login.html')
14+
else:
15+
username = request.form.get('username')
16+
password = request.form.get('password')
17+
if username == 'Zac' and password == '1234':
18+
g.username = 'Zac'
19+
login_log()
20+
return '登录成功!'
21+
else:
22+
return '帐号密码错误!'
23+
24+
if __name__ == '__main__':
25+
app.run(debug=True)
26+
login_log()

g_demo/templates/login.html

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<form action="" method="post">
9+
<table>
10+
<tbody>
11+
<tr>
12+
<td>用户名:</td>
13+
<td><input type="text" name="username"></td>
14+
</tr>
15+
<tr>
16+
<td>密码:</td>
17+
<td><input type="password" name="password"></td>
18+
</tr>
19+
<tr>
20+
<td></td>
21+
<td><input type="submit" value="登录"></td>
22+
</tr>
23+
</tbody>
24+
</table>
25+
</form>
26+
</body>
27+
</html>

g_demo/utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from flask import g
2+
3+
def login_log():
4+
print('当前登录用户为: %s' % g.username)

get_post_demo/get_post_demo.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from flask import Flask, render_template, request
2+
3+
app = Flask(__name__)
4+
5+
6+
@app.route('/')
7+
def hello_world():
8+
return render_template('index.html')
9+
10+
@app.route('/search/')
11+
def search():
12+
print(request.args)
13+
return 'search'
14+
15+
@app.route('/login/', methods=['GET', 'POST'])
16+
def login():
17+
if request.method == 'GET':
18+
return render_template('login.html')
19+
else:
20+
username = request.form.get('username')
21+
password = request.form.get('password')
22+
print(username, password)
23+
return 'post request'
24+
if __name__ == '__main__':
25+
app.run(debug=True)

get_post_demo/templates/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<a href="{{ url_for('search', q='hello') }}">跳转到搜索页面</a>
9+
</body>
10+
</html>

get_post_demo/templates/login.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Title</title>
6+
</head>
7+
<body>
8+
<form action="{{ url_for('login') }}" method="post">
9+
<table>
10+
<tr>
11+
<td>用户名:</td>
12+
<td><input type="text" placeholder="请输入用户名" name="username"></td>
13+
</tr>
14+
<tr>
15+
<td>密码:</td>
16+
<td><input type="text" placeholder="请输入密码" name="password"></td>
17+
</tr>
18+
<tr>
19+
<td></td>
20+
<td><input type="submit" value="登录"></td>
21+
</tr>
22+
</table>
23+
24+
</form>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)