flask-日料網站搭建-後臺登陸

引言:想使用python的flask框架搭建一個日料網站,主要包含web架構,靜態頁面,後臺系統,交互,今天教你們實現後臺登陸功能,比較簡單。html

本節知識:表單標籤,表單驗證,數據查詢,模板python

python環境:python2.7,flask,以及flask相關的庫(沒有列完,若是運行manage.py時提示未安裝的庫安裝就ok了。)git

上節已經把admin(後臺用戶表)表建好了,再看看模型吧。github

-----models.pyweb

class Admin(db.Model): __tablename__ = 'admin' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True, index=True) password = db.Column(db.String(128)) ###權限 admin=1 1>2>3
    power  = db.Column(db.Integer) set_time = db.Column(db.DateTime) set_user = db.Column(db.Integer) email = db.Column(db.String(128)) phone = db.Column(db.String(128)) ''' def __init__(self, username=None, password=None, power=None,phone=phone): self.username = username self.password = password self.power = power self.phone = phone '''
    def __repr__(self): return '<User %r>' % self.username, def check_user(self,username,password): obj = self.query.filter_by(username=username,password=password).first() return obj def check_user_aru(self,username): obj = self.query.filter_by(username=username).first() return obj def get_auser(self): obj = db.session.execute('select b.* ,a.username as set_user_name from admin as a ,admin as b where a.id =b.set_user ').fetchall() return obj

上面大部分是admin表的字段定義內容,下面的函數是我之後會用到的函數。sql

雖然字段有點多,不過咱們只須要用到username和password就好了,肯定了表單字段,接下來就要生成login頁面。數據庫

-----先在視圖文件定義  views.py   這不是完整版,由於還須要定義表單類,下面的LoginForm 還未寫出來。flask

@main.route('/admin/login', methods=['GET', 'POST']) def Alogin(): form = LoginForm() if request.method=='POST': if form.validate_on_submit(): username = form.username.data password = form.password.data ####查詢數據
            try: obj = Admin.check_user(Admin(),username, password) if obj: session['username'] = username session['userid'] = obj.id ###記入adminlog
                    session['login_time'] = int(time.time()) login_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) ip = get_ip() log_list = AdminLog(username=username,userid=obj.id,login_time=login_time,ip=ip,email=obj.email, phone=obj.phone,power=obj.power,loginout_time='') db.session.add(log_list) db.session.commit() log_id = log_list.id session['log_id'] = log_id return redirect('/admin/') except: return render_template('admin/login.html',form=form,errors_info=u'用戶名或密碼錯誤!!') else: return render_template('admin/login.html',form=form,error=form.errors) else: if session.has_key('username'): return redirect('/admin/') return render_template('admin/login.html',form=form)

上述代碼已經寫出大概邏輯了,接下來是login.html以及_formhelpers1.html的內容session

-----login.html   只看表單部分就行了架構

{% from "_formhelpers.html" import render_field %} <form id="loginform" class="form-vertical" action="/admin/login" method="post"> {{ form.hidden_tag() }} <div class="control-group normal_text"> <h3><img src="{{url_for('static', filename='images/admin/logo.png')}}" alt="Logo" /></h3></div>
                <div class="control-group">
                    <div class="controls">
                        <div class="main_input_box">
                            <span class="add-on bg_lg"><i class="icon-user"></i></span>{{ render_field(form.username) }} </div>
                    </div>
                </div>
                <div class="control-group">
                    <div class="controls">
                        <div class="main_input_box">
                            <span class="add-on bg_ly"><i class="icon-lock"></i></span>{{ render_field(form.password) }} </div>
                    </div>
                </div> {% if errors_info %} <span style="margin-left:26px; color:red">{{ errors_info }}</span>{% endif %} <div class="form-actions">
                    <span class="pull-left"><a href="#" class="flip-link btn btn-info" id="to-recover">Lost password?</a></span>
                    <span class="pull-right"><input type="submit" class="btn btn-success" value="Login"/> </span>
                </div>
            </form>

從_formhelpers1.html引入字段,賦值到form標籤裏面,這裏須要注意_formhelpers1.html放置的路徑,對應就ok了。

------_formhelpers1.html

{% macro render_field(field) %} {{ field(**kwargs)|safe }} {% if field.errors %} {% for error in field.errors %} <span for="required" generated="true" class="help-inline">{{ error }}</span> {% endfor %} {% endif %} {% endmacro %}

該文件的內容就是定義的表單標籤,以及輸出錯誤的信息(error)。

接下來上完整版views.py  加上LoginForm

class LoginForm(FlaskForm): username = StringField('Username', validators=[Length(min=3, max=25,message=u'請輸入3-25個字符!')],render_kw={"placeholder": "username",}) password = PasswordField('Password',validators= [ Length(min=3, max=25,message=u'請輸入4-25個字符!')], render_kw={"placeholder": "password",} ) class AuserForm(FlaskForm): username = StringField('username',validators=[Length(min=3, max=25,message=u'請輸入3-25個字符!')],) password = PasswordField('password',validators= [ Length(min=3, max=25,message=u'請輸入4-25個字符!')], ) repassword = PasswordField('repassword',validators= [ Length(min=3, max=25,message=u'請輸入4-25個字符!')], ) email = StringField('email', validators=[Length(min=3, max=25,message=u'請輸入3-25個字符!')],) phone = StringField('phone', validators=[Length(min=3, max=25,message=u'請輸入3-25個字符!')],) power = SelectField('power',choices=[(2,u'管理帳戶'),(3,u'普通帳戶')],coerce=int) @main.route('/', methods=['GET', 'POST']) def index(): return render_template('index.html') @main.route('/admin/login', methods=['GET', 'POST']) def Alogin(): form = LoginForm() if request.method=='POST': if form.validate_on_submit(): username = form.username.data password = form.password.data ####查詢數據
            try: obj = Admin.check_user(Admin(),username, password) if obj: session['username'] = username session['userid'] = obj.id ###記入adminlog
                    session['login_time'] = int(time.time()) login_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) ip = get_ip() log_list = AdminLog(username=username,userid=obj.id,login_time=login_time,ip=ip,email=obj.email, phone=obj.phone,power=obj.power,loginout_time='') db.session.add(log_list) db.session.commit() log_id = log_list.id session['log_id'] = log_id return redirect('/admin/') except: return render_template('admin/login.html',form=form,errors_info=u'用戶名或密碼錯誤!!') else: return render_template('admin/login.html',form=form,error=form.errors) else: if session.has_key('username'): return redirect('/admin/') return render_template('admin/login.html',form=form)

認真看下class LoinForm 怎麼定義字段的照葫蘆畫瓢就ok了。

----------------------這是分割線----------------------------

沒有報錯的話如今打開頁面是ok的

----先測試下,表單字段的驗證有沒有問題

 當我直接點擊登陸,沒有填寫字段名,確定是通不過驗證的

符合class LoginForm 設置的錯誤信息,驗證還有不少方式,網上查閱下資料便可。

----------接下來咱們來看看views.py 登陸的邏輯,基本大功告成了

 

@main.route('/admin/login', methods=['GET', 'POST'])##定義能夠請求的方式
def Alogin(): form = LoginForm()##實例登陸類,傳到頁面
    if request.method=='POST':  ##提交方式
        if form.validate_on_submit():  ##能夠經過驗證
            username = form.username.data   ##獲取表單數據
            password = form.password.data ####查詢數據
            try: obj = Admin.check_user(Admin(),username, password) ##開始查詢
                if obj: session['username'] = username session['userid'] = obj.id ###記入adminlog
                    session['login_time'] = int(time.time()) login_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) ip = get_ip() log_list = AdminLog(username=username,userid=obj.id,login_time=login_time,ip=ip,email=obj.email, phone=obj.phone,power=obj.power,loginout_time='') db.session.add(log_list) db.session.commit() log_id = log_list.id session['log_id'] = log_id return redirect('/admin/') except: return render_template('admin/login.html',form=form,errors_info=u'用戶名或密碼錯誤!!')  ##傳入本身設置的error信息
                
        else: return render_template('admin/login.html',form=form,error=form.errors) else: if session.has_key('username'):    ##登陸時須要判斷用戶是否已經登錄了,固然還有其餘,別入不一樣的ip繼續登陸,就要把他擠下去等
            return redirect('/admin/') return render_template('admin/login.html',form=form)

裏面的check_user函數,正式models.py裏面定義的

固然不定義在這裏也能夠,後期還須要規整,關於數據庫的操做還有有些些麻煩,搞得我每次都想用原生sql來搞了,網上資料也老是對不上個人點,憂傷。

下次根據後臺的功能,再分享下增刪改查的操做,你們一塊兒學習。

暫時沒有把項目上傳到github上面,之後我再傳。

相關文章
相關標籤/搜索