//完全跳过IF语句,但是当我删除ELSE语句时,IF语句有效吗?
if fname != '' and lname != '' and user != '' and rank != '' and password !=
'' and firenum != '' and phonenum != '' and phoneconfirm != '' and admin !=
'':
if phonenum == phoneconfirm and len(phonenum) == int(10):
get = AllPersonnel.query.filter_by(agencyid=current_user.agencyid).all()
for i in get:
if phonenum == i.phonenum:
all = []
all.append({'error' : 'ERROR: Phone number already exists in database'})
return jsonify(all)
else:
pers = AllPersonnel(name.upper(), rank.upper(), firenum, phonenum, username, password, admin.upper(), agencyid, agencyname.upper(), availability, avtimestamp, enat, enattimestamp, taskavailbility, avtimetsamptask, plusdate)
db.session.add(pers)
db.session.commit()
all = []
all.append({'success' : 'SUCCESS: FireFighter added to database!'})
return jsonify(all)
这是我的其他代码,适用于我的其他程序。
phoneemp = EmsaEmp.query.all()
for num in phoneemp:
if phonenum == num.phonenum:
allexist = ('ERROR: Phone Number Already Exists In Database. Please Use The Help Desk or Contact a Supervisor. Thanks')
return render_template('register.html', allexist=allexist)
else:
print('Success')
ans = ''
name = (fname + ' ' + lname)
emp = EmsaEmp(name.upper(), title.upper(), phonenum, ans)
db.session.add(emp)
db.session.commit()
这就是我最终做的事情,它现在有效。
if phonenum == phoneconfirm and len(phonenum) == int(10):
get = AllPersonnel.query.filter_by(agencyid=current_user.agencyid).all()
for i in get:
if phonenum == i.phonenum:
all = []
all.append({'error' : 'ERROR: Phone number already exists in database'})
return jsonify(all)
for u in get:
if phonenum != i.phonenum:
pers = AllPersonnel(name.upper(), rank.upper(), firenum, phonenum, username, password, admin.upper(), agencyid, agencyname.upper(), availability, avtimestamp, enat, enattimestamp, taskavailbility, avtimetsamptask, plusdate)
db.session.add(pers)
db.session.commit()
all = []
all.append({'success' : 'SUCCESS: FireFighter added to database!'})
return jsonify(all)
答案 0 :(得分:5)
你在一个循环中。
因此,在第一个元素上,您将获得else
,然后是return
,并且永远不会执行后续元素。
如果您删除其他内容,则会让您的算法有机会执行后续的if
语句。
我说你的算法不能做你想做的事情; - )。
此外,用
替换你的巨大if
if all((fname, lname, user, rank, password, firenum, phonenum, phoneconfirm, admin)):
此处有更多信息:https://docs.python.org/3.6/library/functions.html#all
无需len(phonenum) == int(10)
。只需len(phonenum) == 10
就足够了(Python 至少,就像其他语言一样简单,但不简单)
另一个问题:
all = []
all.append({'error' : 'ERROR: Phone number already exists in database'})
return jsonify(all)
这可以改写为:
return jsonify([{'error' : 'ERROR: Phone number already exists in database'}])
这样你就不再创造一个临时变量了(这不是这3行的唯一问题,但我会停在那里)。
请尝试以下代码:
import collections
PersonnelData = collections.namedtuple('PersonnelData', [
'fname', 'lname', 'user', 'rank', 'password',
'firenum', 'phonenum', 'phoneconfirm', 'admin',
])
@app.route('/addpersonnellist', methods=['GET', 'POST'])
def addpersonnellist():
data = PersonnelData(
fname=request.args.get('fname'),
lname=request.args.get('lname'),
username=request.args.get('user'),
rank=request.args.get('rank'),
password=request.args.get('password'),
firenum=request.args.get('firenumone'),
phonenum=request.args.get('phonenum'),
phoneconfirm=request.args.get('phonenumconfirm'),
admin=request.args.get('admin'),
)
name = '{data.fname} {data.lname}'.format(data=data)
if all(data):
if phonenum == phoneconfirm and len(phonenum) == 10:
personnel_phonenum = AllPersonnel.objects.values_list('phonenum', flat=True)
if data.phonenum in personnel_phonenum:
response = 'ERROR: Phone number already exists in database'
else:
pers = AllPersonnel(
name.upper(), data.rank.upper(), data.firenum, data.phonenum,
data.username, data.password, data.admin.upper(),
current_user.agencyid, current_user.agencyname.upper(),
'', '', '', '', '', '', '',
)
db.session.add(pers)
db.session.commit() # Can you do a commit when you're not in a transaction???
response = 'SUCCESS: FireFighter added to database!'
else:
response = 'ERROR: Phone numbers do not match and/or format is invalid.'
else:
response = 'ERROR: Not all fields filled out correctly!'
return jsonify({'response' : response})
正如您所注意到的那样,没有for循环。尝试使用实际的API。花时间阅读文档。这不是浪费时间。
请检查:https://docs.djangoproject.com/en/1.11/ref/models/querysets/#values-list
单return
方式的另一种替代方法是使用保护条款:https://refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html
import collections
PersonnelData = collections.namedtuple('PersonnelData', [
'fname', 'lname', 'user', 'rank', 'password',
'firenum', 'phonenum', 'phoneconfirm', 'admin',
])
@app.route('/addpersonnellist', methods=['GET', 'POST'])
def addpersonnellist():
data = PersonnelData(
fname=request.args.get('fname'),
lname=request.args.get('lname'),
username=request.args.get('user'),
rank=request.args.get('rank'),
password=request.args.get('password'),
firenum=request.args.get('firenumone'),
phonenum=request.args.get('phonenum'),
phoneconfirm=request.args.get('phonenumconfirm'),
admin=request.args.get('admin'),
)
if not all(data):
return jsonify({'response' : 'ERROR: Not all fields filled out correctly!'})
if not phonenum == phoneconfirm or len(phonenum) != 10:
return jsonify({'response' : 'ERROR: Phone numbers do not match and/or format is invalid.'})
personnel_phonenum = AllPersonnel.objects.values_list('phonenum', flat=True)
if data.phonenum in personnel_phonenum:
return jsonify({'response' : 'ERROR: Phone number already exists in database'})
pers = AllPersonnel(
'{data.fname} {data.lname}'.format(data=data).upper(),
data.rank.upper(), data.firenum, data.phonenum,
data.username, data.password, data.admin.upper(),
current_user.agencyid, current_user.agencyname.upper(),
'', '', '', '', '', '', '',
)
db.session.add(pers)
db.session.commit() # Can you do a commit when you're not in a transaction???
return jsonify({'response' : 'SUCCESS: FireFighter added to database!'})