我正在处理一个烧瓶文件,我正在尝试将一些功能和方法放在一个单独的python文件中,以便更容易使用并创建一个仅用于我的功能的文件烧瓶应用。我已经查找了怎么做,但由于某种原因,它不与我合作,我希望有人可以帮助我。我有下面的文件片段和github链接。
github:https://github.com/omar-jandali/RoadTripWebsite
原始 init .py文件:
import modu
from flask import Flask, render_template, request, redirect, url_for, flash
from flask import escape, session
from datetime import datetime
from sqlalchemy import *
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Users, Groups, Records, Base
@app.route('/validate_registration', methods=['GET', 'POST'])
def Validate_Registration():
usernameMinChars = 4
usernameMaxChars = 12
passwordMinChars = 8
passwordMaxChars = 14
if request.method == 'POST':
# this is all of the fields in the form
username = request.form['username']
email = request.form['email']
password = request.form['password']
verify = request.form['verify']
check_username = db_session.query(Users).filter_by(Username = username).\
first()
check_email = db_session.query(Users).filter_by(Email = email).first()
if username != None:
if len(username) >= usernameMinChars:
if len(username) <= usernameMaxChars:
if check_username == None:
valid_username = username
else:
flash('The username ' + username + ' is already in use')
return redirect(url_for('Registration'))
else:
flash('The username is too long')
return redirect(url_for('Registration'))
else:
flash('The username is too short')
return redirect(url_for('Registration'))
else:
flash('Youh ave to enter a username to register')
return redirect(url_for('Registration'))
更改了服务器/ init .py文件(我想对其进行查询,以便我在另一个文件中调用def以验证用户名并在有效时提交用户名。)
替换:
if username != None:
if len(username) >= usernameMinChars:
if len(username) <= usernameMaxChars:
if check_username == None:
valid_username = username
else:
flash('The username ' + username + ' is already in use')
return redirect(url_for('Registration'))
else:
flash('The username is too long')
return redirect(url_for('Registration'))
else:
flash('The username is too short')
return redirect(url_for('Registration'))
else:
flash('Youh ave to enter a username to register')
return redirect(url_for('Registration'))
使用:
valid_username = modu.ValidateUsername(check_username)
**我在 init .py文件顶部导入的同一文件目录中添加了一个名为modu的新文件
这是为了保存所有defs而创建的modu.py文件,可以在需要时由 init .py文件调用:
from flask import Flask, render_template, request, redirect, url_for, flash
from flask import escape, session
from datetime import datetime
from sqlalchemy import *
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Users, Groups, Records, Base
def ValidateUsername(username):
if username != None:
if len(username) >= usernameMinChars:
if len(username) <= usernameMaxChars:
if check_username == None:
valid_username = username
return valid_username
else:
flash('The username ' + username + ' is already in use')
return redirect(url_for('Registration'))
else:
flash('The username is too long')
return redirect(url_for('Registration'))
else:
flash('The username is too short')
return redirect(url_for('Registration'))
else:
flash('Youh ave to enter a username to register')
return redirect(url_for('Registration'))
答案 0 :(得分:0)
首先,当你不必
时,不要试图嵌套东西其次,你试图返回两个不同的对象,所以你需要抓住那个
def is_valid_user(username):
go_register = False
if not username:
flash('You have to enter a username to register')
go_register = True
if len(username) >= usernameMinChars:
flash('The username is too long')
go_register = True
if len(username) <= usernameMaxChars:
flash('The username is too short')
go_register = True
if not check_username:
flash('The username ' + username + ' is already in use')
go_register = True
# return two items, a redirect and the username
if go_register:
return redirect(url_for('Registration')), None
# If there was no error, you don't redirect
return None, username
例如,
go_register, valid_username = is_valid_user(username)
if go_register:
return go_register
else:
flash("valid " + valid_username)