我正在嘗試使用Flask-Security來登錄和註冊用戶。但是,我試圖做兩件事。首先是將默認/註冊更改爲/註冊,我相信我已經正確完成了。 第二是我想自定義註冊表單。我可以加載頁面,但每當我提交表單時都不會發生任何事情。沒有數據發送到後端或插入到sqlite數據庫。Flask-Security:自定義註冊
我也不確定是否需要編寫我自己的app.route函數來處理創建新用戶和/或檢查它們是否已經存在。我希望能夠使用flask-security的註冊表,但只是調整它。如果這甚至是可能的。
這裏是我主要的Python文件:
from flask import Flask, redirect, flash, session, request, render_template
from flask_sqlalchemy import SQLAlchemy
import os, time
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['DATABASE'] = 'data.db'
app.config['SECURITY_TRACKABLE'] = True
app.config['SECURITY_REGISTERABLE'] = True
app.config['SECURITY_REGISTER_URL'] = '/signup'
app.config['SECURITY_REGISTER_USER_TEMPLATE'] = 'security/register.html'
# enable this if you want to send email and confirm
# app.config['SECURITY_CONFIRMABLE'] = True
db = SQLAlchemy(app)
from user import Security, User, user_datastore
from forms import logform, signupform, RegisterForm
security = Security(app, user_datastore, register_form=signupform, login_form=logform,
confirm_register_form=signupform)
db.create_all()
db.session.commit()
@app.route('/')
def hello_world():
log = logform(csrf_enabled=False)
flash("HELLO.")
return render_template("index.html", form=log)
@app.route('/login')
def login():
print "/LOGIN REQUESTED"
form = logform(csrf_enabled=False)
return render_template("security/login_user.html", login_user_form=form, security=security)
#not sure if I need this function at all
@app.route('/signup', methods=['GET', 'POST'])
def signup():
print "/SIGNUP REQUESTED"
form = signupform(csrf_enabled=False)
# form = RegisterForm()
if request.method == 'GET':
return render_template("security/register.html", register_user_form=form, security=security)
else:
#log = logform(csrf_enabled=False)
if form.validate_on_submit() or form.validate():
print "HERE NOW SOMEONE SUBMITTED SOMETHING"
print form.email.data
print form.username.data
print form.password.data
user_datastore.create_user(form)
db.session.commit()
return redirect('/')
我不知道如果我需要在/註冊功能或瓶的安全性將處理它的默認值。 接下來這裏是我的logform和signupform類:
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo
from flask_security.forms import LoginForm, RegisterForm, Form
class logform(LoginForm):
login = StringField('login', validators=[DataRequired()])
# password = PasswordField('password', validators=[DataRequired()])
class signupform(RegisterForm):
# email = StringField('email', validators=[Email(), DataRequired()])
username = StringField('username', [DataRequired()])
# password = PasswordField('password', validators=[DataRequired()])
這裏是我試圖使用當前的登記表。我不確定是否應該使用url_for('註冊')作爲flask-security的默認註冊表單使用url_for_security('register')。
<!DOCTYPE html>
{% extends "base.html" %}
{% block content %}
<div id="signup">
<h1>Create Account.</h1>
<h3>TESTING</h3>
{# not sure what to put as my action. default flask-security uses url_for_security('register') #}
<form id="logfrm" name='register_form' method="POST" action="{{ url_for('signup') }}">
{# figure out how to make input required #}
{{ register_user_form.email(placeholder="email", type="email") }}<br>
{{ register_user_form.username(placeholder="username") }}<br>
{{ register_user_form.password(placeholder="password", type="password") }}<br>
{# register_user_form.confirm(placeholder="confirm password") #}<br>
{{ register_user_form.submit }}
<input type="submit" value="SIGNUP" >
</form>
</div>
{% endblock %}
任何幫助將是偉大的,謝謝!
感謝您的幫助。這真的解決了我遇到的大部分麻煩。 唯一的錯誤是我的形式。我忘記了RegisterForm
需要一個密碼確認,所以我也補充道: {{ register_user_form.password_confirm(...) }}
,並還包括app.config['SECRET_KEY'] = 'blah'
在我的主文件,以便登記不運行的錯誤。
一個問題。你如何看待你可以重寫的方法?我想在'RegisterForm'中更改'password_confirm'的desc和名稱,但它看起來不像是在工作。 – Scragglez
@Scragglez將此作爲另一個問題發佈。 – pjcunningham