我的全局变量返回的值不一致。我将感谢您的指导。
我有一个列表屏幕,可以按用户名,姓氏或姓氏的顺序排列。通过分页,它将一次列出几个,然后我可以翻页/下一页或翻页/首页。如果我从一个标准更改为另一个标准,则它将直接转到列表的第1页,否则它将根据需要提供下一个/最后一个/上一个/第一个。为了让页面记住顺序(用户名/姓氏/姓氏),我将其保存在全局变量member_filter_criteria中。
在我的PC上,此代码可以很好地与下面的代码配合使用,当我上传到Heroku时,member_filter_criteria值会在页面之间随机变化。您可以在运行的应用程序中看到这一点:首先在https://clubmanager-2020ad.herokuapp.com/auth/login?next=%2Findex上以Janice(密码“ cat”)登录,然后输入https://clubmanager-2020ad.herokuapp.com/testmembers。
我怀疑我使用或引用的全局参数不正确,但是有人可以告诉我正确的方法吗?
相关代码:
1)我的routes.py文件的顶部,紧随导入之后
...
member_filter_criteria = "Surname"
like = '%'
....
2 route.py中的def是:
@bp.route('/testmembers/', methods=['GET', 'POST'])
@login_required
def testmembers():
# I need to retrieve the value of member_filter_criteria and like from the last call to this def
# This is why I have declared them as global
global member_filter_criteria
global like
def printC(answer):
return "\n{:0.2f}.\n".format(answer)
form = MemberForm()
form.fieldtype.choices = [('--', '--'), ('User Name', 'User Name'), ('Forename', 'Forename'),
('Surname', 'Surname')]
user = Users.query.filter_by(username=current_user.username).first_or_404()
strbal = printC(user.balance / 100)
club = Club.query.filter_by(clubnum=current_user.club).first_or_404()
clubname = club.clubname
todaydate = date.today()
# If a new query we must set the pagination page to 1 at the end of this def
# Otherwise we use the current page number
newquery = 'No'
if form.validate_on_submit():
newquery = 'Yes'
like = form.searchletter.data.upper() + '%'
if form.fieldtype.data == 'User Name':
member_filter_criteria = 'User Name'
users_list = Users.query.filter_by(club=club.clubnum).filter(Users.username.like(like)).order_by(
Users.username.asc())
if form.fieldtype.data == 'Forename':
member_filter_criteria = 'Forename'
users_list = Users.query.filter_by(club=club.clubnum).filter(Users.forename.like(like)).order_by(
Users.forename.asc())
if form.fieldtype.data == 'Surname':
member_filter_criteria = 'Surname'
users_list = Users.query.filter_by(club=club.clubnum).filter(Users.surname.like(like)).order_by(
Users.surname.asc(), Users.forename.asc())
if form.fieldtype.data == '--':
users_list = Users.query.filter_by(club=club.clubnum).filter(Users.forename.like(like)) \
.order_by(Users.surname.asc(), Users.forename.asc())
else:
form.fieldtype.data = member_filter_criteria
if like == '%%':
like = '%'
form.searchletter.data = like
if member_filter_criteria == '':
users_list = Users.query.filter_by(club=club.clubnum).order_by(Users.surname.asc(), Users.forename.asc())
if member_filter_criteria == 'User Name':
users_list = Users.query.filter_by(club=club.clubnum).filter(Users.username.like(like)).order_by(
Users.username.asc())
if member_filter_criteria == 'Forename':
users_list = Users.query.filter_by(club=club.clubnum).filter(Users.forename.like(like)).order_by(
Users.forename.asc())
if member_filter_criteria == 'Surname':
users_list = Users.query.filter_by(club=club.clubnum).filter(Users.surname.like(like)).order_by(
Users.surname.asc(),
Users.forename.asc())
if member_filter_criteria == '--':
users_list = Users.query.filter_by(club=club.clubnum).filter(Users.forename.like(like)) \
.order_by(Users.surname.asc(), Users.forename.asc())
paginator = Paginator(users_list, club.items_per_page)
if newquery == 'Yes':
page = 1
else:
page = request.args.get('page', type=int)
paginatelist = paginator.get_page(page)
return render_template('member.html', title=_('Members'),
club=club, paginator=paginator, paginatelist=paginatelist,
form=form)
3forms.py中的表单(更改过滤器标准是:)>
class MemberForm(FlaskForm): searchletter = StringField(_l('Choose One or more letters of name') ) fieldtype = SelectField(_l('Search on: User Name<br>User Forename<br>User Surname')) submit = SubmitField(_l('Submit'))
4 HTML屏幕是:
{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block app_content %}
<h1>{{ _('Member details')}} </h1>
{% if current_user.adminuser == 'Yes' or current_user.coach == 'Yes' or
current_user.treasurer == 'Yes' or current_user.sessionmanager == 'Yes' %}
{% if form %}
{{ wtf.quick_form(form) }}
<br>
{% endif %}
{% endif %}
<table class="table table-hover">
<tr>
<th>Username</th>
<th>User Number</th>
<th>Forename</th>
<th>Surname</th>
<th>Member Type</th>
<th>Balance</th>
<th>Current<br>Member</th>
<th>Member<br>Expires</th>
<th>Registered<br>Member</th>
<!-- <th>Administrator</th>
<th>Coach</th>
<th>Activity <br> Manager</th>
<th>Treasurer</th> -->
</tr>
{% for user in paginatelist %}
{% include '_user.html' %}
{% endfor %}
{% include '_paginate.html' %}
</table>
{% include '_paginate.html' %}
{% endblock %}
我的全局变量返回的值不一致。我将不胜感激。我有一个列表屏幕,可以按用户名,姓氏或姓氏的顺序排列。通过分页将...
Grismar向我指出了正确的方向-不要在Web应用程序中使用Global变量,因为它们是无状态的,而且不可能知道哪个线程将接收请求。