码迷,mamicode.com
首页 > 编程语言 > 详细

轻量Pythonweb - flask+jinja2

时间:2019-03-18 13:52:16      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:import   post   orm   代码   ima   http   ace   main   ==   

后台代码 MVC

from flask import Flask,request,render_template

app = Flask(__name__)

@app.route('/',methods=['GET','POST'])
def home():
    return render_template('home.html')

@app.route('/signin',methods=['GET'])
def signin_form():
    return render_template('form.html')

@app.route('/signin',methods=['POST'])
def sigin():
    username = request.form['username']
    password = request.form['password']
    if username.strip() == 'admin' and password.strip() == 'password':
        return render_template('signin-ok.html',username=username)
    return render_template('form.html',message='Bad information...',username=username)

if __name__ == '__main__':
    app.run()

View 都放到 templates 目录下

技术图片

<html>
<head>
  <title>Home</title>
</head>
<body>
  <h1 style="font-style:italic">Home</h1>
</body>
</html>
<html>
<head>
  <title>Please Sign In</title>
</head>
<body>
  {% if message %}
  <p style="color:red">{{ message }}</p>
  {% endif %}
  <form action="/signin" method="post">
    <legend>Please sign in:</legend>
    <p><input name="username" placeholder="Username" value="{{ username }}"></p>
    <p><input name="password" placeholder="Password" type="password"></p>
    <p><button type="submit">Sign In</button></p>
  </form>
</body>
</html>
<html>
<head>
  <title>Welcome, {{ username }}</title>
</head>
<body>
  <p>Welcome, {{ username }}!</p>
</body>
</html>

轻量Pythonweb - flask+jinja2

标签:import   post   orm   代码   ima   http   ace   main   ==   

原文地址:https://www.cnblogs.com/Frank99/p/10551483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!