用的session来保存信息
在templates文件夹下添加html文件
layout.html
1 |
|
2 | <title>Flaskr</title> |
3 | <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}"> |
4 | <div class=page> |
5 | <h1>Flaskr</h1> |
6 | <div class=metanav> |
7 | {% if not session.logged_in %} |
8 | <a href="{{ url_for('login') }}">log in</a> |
9 | {% else %} |
10 | <a href="{{ url_for('logout') }}">log out</a> |
11 | {% endif %} |
12 | </div> |
13 | {% for message in get_flashed_messages() %} |
14 | <div class=flash>{{ message }}</div> |
15 | {% endfor %} |
16 | {% block body %}{% endblock %} |
17 | </div> |
login.html
1 | {% extends "layout.html" %} |
2 | {% block body %} |
3 | <h2>Login</h2> |
4 | {% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %} |
5 | <form action="{{ url_for('login') }}" method=post> |
6 | <dl> |
7 | <dt>Username: |
8 | <dd><input type=text name=username> |
9 | <dt>Password: |
10 | <dd><input type=password name=password> |
11 | <dd><input type=submit value=Login> |
12 | </dl> |
13 | </form> |
14 | {% endblock %} |
show_entries.html
1 | {% extends "layout.html" %} |
2 | {% block body %} |
3 | {% if session.logged_in %} |
4 | <form action="{{ url_for('add_entry') }}" method='POST' class=add-entry> |
5 | <dl> |
6 | <dt>Title: |
7 | <dd><input type=text size=30 name=title> |
8 | <dt>Text: |
9 | <dd><textarea name=text rows=5 cols=40></textarea> |
10 | <dd><input type=submit value=Share> |
11 | </dl> |
12 | </form> |
13 | {% endif %} |
14 | <ul class=entries> |
15 | {% for entry in entries %} |
16 | <li><h2>{{ entry.title }}</h2>{{ entry.content|safe }} |
17 | {% else %} |
18 | <li><em>Unbelievable. No entries here so far</em> |
19 | {% endfor %} |
20 | </ul> |
21 | {% endblock %} |
在static下CSS文件
style.css
1 | body { font-family: sans-serif; background: #eee; } |
2 | a, h1, h2 { color: #377BA8; } |
3 | h1, h2 { font-family: 'Georgia', serif; margin: 0; } |
4 | h1 { border-bottom: 2px solid #eee; } |
5 | h2 { font-size: 1.2em; } |
6 | |
7 | .page { margin: 2em auto; width: 35em; border: 5px solid #ccc; |
8 | padding: 0.8em; background: white; } |
9 | .entries { list-style: none; margin: 0; padding: 0; } |
10 | .entries li { margin: 0.8em 1.2em; } |
11 | .entries li h2 { margin-left: -1em; } |
12 | .add-entry { font-size: 0.9em; border-bottom: 1px solid #ccc; } |
13 | .add-entry dl { font-weight: bold; } |
14 | .metanav { text-align: right; font-size: 0.8em; padding: 0.3em; |
15 | margin-bottom: 1em; background: #fafafa; } |
16 | .flash { background: #CEE5F5; padding: 0.5em; |
17 | border: 1px solid #AACBE2; } |
18 | .error { background: #F0D6D6; padding: 0.5em; } |
在controller目录下新建blog_message.py文件
其实也就是接口文件= =
1 | #!/usr/bin/env python3 |
2 | # _*_ coding: utf-8 _*_ |
3 | # @Time : 2020/3/13 11:32 |
4 | # @Author: ChenZIDu |
5 | # @File : blog_message.py |
6 | |
7 | from blog.model.User import User |
8 | from blog.model.Category import Category |
9 | import os |
10 | |
11 | from blog import app,db |
12 | from flask import request,render_template,flash,abort,url_for,redirect,session,Flask,g |
13 | |
14 |
|
15 | def show_entries(): # 从数据库查询出文章的标题和正文。 |
16 | categorys = Category.query.all() |
17 | return render_template('show_entries.html',entries=categorys) |
18 | |
19 |
|
20 | def add_entry(): |
21 | if not session.get('logged_in'): |
22 | abort(401) # 请求到此即中断,不会打印下面的语句,并返回HTTP状态码401 |
23 | title = request.form['title'] |
24 | content = request.form['text'] |
25 | category = Category(title, content) |
26 | db.session.add(category) |
27 | db.session.commit() |
28 | flash('New entry was successfully posted') |
29 | return redirect(url_for('show_entries')) |
30 | |
31 |
|
32 | def login(): |
33 | error = None |
34 | if request.method == 'POST': |
35 | username = request.form['username'] |
36 | password = request.form['password'] |
37 | user = User.query.filter_by(username=request.form['username']).first() |
38 | passwd = User.query.filter_by(password = request.form['password']).first() |
39 | |
40 | if user is None: |
41 | error = 'Invalid username' |
42 | elif password is None: |
43 | error = 'Invalid password' |
44 | else: |
45 | session['logged_in'] = True |
46 | flash('You were logged in') |
47 | return redirect(url_for('show_entries')) #url_for() 来针对一个特定的函数构建一个 URL |
48 | return render_template('login.html', error=error) |
49 | |
50 |
|
51 | def logout(): |
52 | session.pop('logged_in', None) |
53 | flash('You were logged out') |
54 | return redirect(url_for('show_entries')) |
在__init__.py文件中引入模块(要在app声明后=.=)
from blog.controller import blog_manage
然后就可以运行试试水了~