首頁>技術>

# -*- coding: utf-8 -*-

"""

Created on Fri Jan 31 14:02:45 2020

@author: Administrator

"""

from flask import Flask,redirect,url_for

from flask import request,current_app

from flask import g,views

import json

from login import logins #匯入藍圖物件

app=Flask(__name__)

def auth(func):

print('我在上面')

def inner(*args,**kwargs):

return func(*args,**kwargs)

return inner

class IndexView(views.MethodView): #CBV檢視

methods=['GET'] #允許的http請求方法(改CBV只允許GET方法)

decorators = [auth,] #每次請求過來都加auth裝飾器

def get(self):

return 'Index.GET'

def post(self):

return 'Index.POST'

app.add_url_rule('/index/',view_func=IndexView.as_view(name='name1')) #(name='name1'反向生成url別名

if __name__ == '__main__':

app.run()

#註冊藍圖,logins是藍圖物件,url_prefix是路由路徑,預設是根路由

app.register_blueprint(logins,url_prefix='')

if __name__=='__main__':

app.run('127.0.0.1','5000')

@app.route('/')

def he():

data={'1':1,'2':2}

return json.dumps(data)

@app.route('/sxw')

def sxw():

g.username='hw'

g.password='123'

current_app.us='Zj'

current_app.pa='123'

return 'hello world,'

#請求鉤子

@app.before_first_request

def before_first_request():

return 'hello1'

@app.before_request

def before_request():

return 'hello2'

@app.after_request

def after_request(response):

return response

@app.teardown_request

def teardown_request(error):

return 'hello3,%s'%error

if __name__=='__main__':

app.run('127.0.0.1','5000')

#表單建立與比對

from flask import Flask,render_template, redirect,url_for,session,request,flash

#匯入wtf擴充套件的表單類

from flask_wtf import FlaskForm

#匯入自定義表單需要的欄位

from wtforms import SubmitField,StringField,PasswordField

#匯入wtf擴充套件提供的表單驗證器

from wtforms.validators import DataRequired,EqualTo

app = Flask(__name__)

# 需要設定 SECRET_KEY 的配置引數

app.config['SECRET_KEY']='1'

class Login(FlaskForm):

us = StringField(label=u'使用者:',validators=[DataRequired()])

ps = PasswordField(label=u'密碼',validators=[DataRequired(),EqualTo('ps2','err')])

ps2 = PasswordField(label=u'確認密碼',validators=[DataRequired()])

submit = SubmitField(u'提交')

#定義根路由檢視函式,生成表單物件,獲取表單資料,進行表單資料驗證

@app.route('/',methods=['GET','POST'])

def index():

# 建立一個Login物件

form = Login()

if form.validate_on_submit():

# 呼叫Login物件當中的屬性,並取其數值

name = form.us.data

pswd = form.ps.data

pswd2 = form.ps2.data

print(name,pswd,pswd2)

# 重定向至login的裝飾器

return redirect(url_for('login'))

else:

if request.method=='POST':

flash(u'資訊有誤,請重新輸入!')

return render_template('qw.html',form=form)

if __name__ == '__main__':

app.run()

qw.html

<!DOCTYPE html>

<html>

<head>

<title>

</title>

</head>

<body>

<form method="post">

{{ form.csrf_token() }}

{{ form.us.label }}

<p>{{ form.us }}</p >

{{ form.ps.label }}

<p>{{ form.ps }}</p >

{{ form.ps2.label }}

<p>{{ form.ps2 }}</p >

<p>{{ form.submit() }}</p >

{% for x in get_flashed_messages() %}

{{ x }}

{% endfor %}

</form>

{%macro input(name,value='',type='text',size=20)%}

<input type="{{type}}" name="{{name}}" value="{{value}}" size="{{size}}">

{%endmacro%}

{{input(type="password" value='name' size=40)}}

</body>

</html>

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • nuxtJs跨域