这是app.py文件的模块和配置部分。
from flask import Flask, render_template, flash, request, logging, redirect,
url_for, session
from data import Blogs
from flaskext.mysql import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField,
validators
from passlib.hash import sha256_crypt
mysql = MySQL()
app = Flask(__name__)
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PORT'] = 5000
app.config['MYSQL_DATABASE_PASSWORD'] = '123456'
app.config['MYSQL_DATABASE_DB'] = 'myflaskapp'
app.config['MYSQL_DATABASE_CURSORCLASS'] = 'DictCursor'
mysql.init_app(app)
这是@ app.route部分
@app.route('/register', methods=['GET', 'POST'])
def register():
#have used wtforms
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
id = form.id.data
name = form.name.data
username = form.username.data
email = form.email.data
password = sha256_crypt.encrypt(str(form.password.data))
college = form.college.data
year = form.year.data
# Create cursor
cur = mysql.get_db().cursor()
# Execute query
cur.execute("INSERT INTO users(id, name, username, email, password,
college, year) VALUES(%s, %s, %s, %s, %s, %s, %s)", (id, name,
username, email, password, college, year))
flash('You are now registered and can log in', 'success')
return redirect(url_for('login'))
return render_template('register.html', form=form)
我得到了 方法不允许 请求的URL不允许使用该方法。 请帮我解决这个问题。
这是在macosx上
请帮帮我。 谢谢。
答案 0 :(得分:0)
更改VALUES(%s, %s, %s, %s, %s, %s, %s)", (id, name, username, email, password, college, year))
为:
VALUES('{}', '{}' ,'{}', '{}', '{}', '{}', '{}')".format(id, name, username, email, password, college, year))