2016-07-01 78 views
-1

我使用的瓶制作一个网站,并在下面MySQLdb的是我的Python文件必须是字符串或只读缓冲器,没多久

init.py

from flask import Flask,render_template,request,url_for,flash,session 
from flask_session import Session 
from dbconnect import connection 
from wtforms importForm,BooleanField,StringField,TextField,PasswordField,validators,IntegerField 
from passlib.hash import sha256_crypt 
from MySQLdb import escape_string as thwart 
from flask_wtf import Form 
from wtforms.validators import InputRequired 
import gc 

sess=Session() 
SESSION_TYPE = 'memcache' 
app=Flask(__name__) 

@app.route('/') 
def test(): 
    return render_template("home.html") 


@app.route('/about/') 
def about(): 
    return render_template("about.html") 

@app.route('/dashbord/') 
def dashbord(): 
    return('hello') 


@app.route('/contact/') 
def contact(): 
    return render_template("contact.html") 

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


class RegistrationForm(Form): 
    username=TextField('username',[validators.Length(min=4,max=20),validators.Required()]) 
    email=TextField('email',[validators.Length(min=6,max=50),validators.Required()]) 
    password=PasswordField('password',[validators.EqualTo('confirm',message="Password must match"),validators.Required()]) 
    confirm=PasswordField("repeat password") 
    phone_no=IntegerField('phone_no',[validators.Required()]) 


@app.route("/sign_up",methods=['GET','POST']) 
def sign(): 
    try: 
     form=RegistrationForm(request.form) 
     if request.method == 'POST': 
      username=form.username.data 
      email=form.email.data 
      password=sha256_crypt.encrypt((str(form.password.data))) 
      phone_no=form.phone_no.data 
      c,conn =connection() 

      x = c.execute("SELECT * FROM customer WHERE username =(%s)", 
          (username,)) 

      if int(x)>0: 
       flash("That username is taken") 
       return render_template('sign.html',form=form) 
      else: 
       args="INSERT INTO customer (username,email,password,phone_no) VALUES (%s,%s,%s,%s)", 
       (thwart(username),thwart(email),thwart(password),thwart(phone_no)) 
       c.execute(*args) 

       conn.commit() 
       flash("Thanks for registering") 
       c.close() 
       conn.close() 

       gc.collect() 

       session['logged_in']=True 
       session['username']=username 
       return redirect(url_for('dashbord')) 
     return render_template("sign.html",form=form) 

    except Exception as e: 
     return(str(e)) 



if __name__=="__main__": 
    app.secret_key = 'super secret key' 
    app.config['SESSION_TYPE'] = 'filesystem' 
    sess.init_app(app) 
    app.run(debug=True) 

我收到以下错误点击提交按钮后

Must be string or read-only buffer, not long

+0

请你提供完整的错误信息? –

+0

这只是我得到TypeError:必须是字符串或只读缓冲区,不长。我认为这是与pone_no @LPK –

+0

有关的事情但是,你不知道错误出现的行号吗? –

回答

0

您cursor.execute看起来不正确对我来说这是你的情况c.execute作为可选值有额外的逗号。 请纠正我,如果我错了

+0

请参考以下[链接](http://stackoverflow.com/问题/ 33396856 /烧瓶-未全参数转换-期间串格式化) –

相关问题