计算Flask中的登录尝试次数 [英] Counting login attempts in Flask

查看:205
本文介绍了计算Flask中的登录尝试次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Web开发的新手,这是我尝试过的:

I'm a total newbie to web development, and here is what I've tried:

@app.route('/login', methods=['GET', 'POST'])
def login():
    attempt = 0
    form = LoginForm()
    if form.validate_on_submit():
        flash('Login requested for OpenID="%s", remember_me=%s' %
              (form.openid.data, str(form.remember_me.data)))
        return redirect('/index')
    attempt += 1
    flash('Attempt(s)="%d"' % attempt)
    return render_template('login.html',
                           title='Sign In',
                           form=form)

输出始终为Attempt(s)="1",而我希望每次form.validate_on_submit()失败时该数字都会增加1.

The output is always Attempt(s)="1", while I expect the number to increase by 1 each time form.validate_on_submit() fails.

我观察到,当我按下Sign In按钮时,login页面会刷新.但是,我在文本字段中输入的所有文本都保留在此处,因此我想未执行form = LoginForm().(如果创建了全新的LoginForm,这些文本怎么可能仍然存在?)因此,我将语句<form = LoginForm()上方的c6>,希望每次按下Sign In时都不要执行它,但是显然这是行不通的.

I observed that when I press Sign In button, login page is refreshed. However, all text I inputted in the text fields remained there, so I suppose form = LoginForm() wasn't executed.(If a brand new LoginForm is created, how can these text still be there?) Thus I put the statement attempt = 0 above form = LoginForm(), hoping it not to be executed each time Sign In is pressed, but apparently this does't work.

我认为问题是我不知道按下Sign In时发生了什么.是否再次调用功能login?如果是这样,它是由谁打电话的?

I think the problem is that I don't know what happened when I pressed Sign In. Is the function login called again? If so, it's called by whom?

这是login.html的内容:

{% extends "base.html" %}

{% block content %}
  <h1>Sign In</h1>
  <form action="" method="post" name="login">
      {{ form.hidden_tag() }}
      <p>
          Please enter your OpenID:<br>
          {{ form.openid(size=80) }}<br>
      </p>
      <p>{{ form.remember_me }} Remember Me</p>
      <p><input type="submit" value="Sign In"></p>
  </form>
{% endblock %}

推荐答案

网络是无状态的.每次您按下提交按钮时,浏览器都会调用与该表单关联的URL.该URL被路由到您的login()处理函数,该函数从头开始运行.该函数本身不存储以前的调用(也不希望这样做,否则站点的所有用户都将获得相同的登录计数).表单由提交请求中发送的数据填充,并由flask-wtf库自动发送到表单实例.

The web is stateless. Every time you press a submit button, your browser calls the URL associated with that form; that URL is routed to your login() handler function, which runs from the start. The function itself has no memory of previous calls (and nor would you want it to, otherwise all users of your site would get the same login count). The form is populated from the data sent in the submit request, which is sent automatically to the form instantation by the flask-wtf library.

您需要在两次通话之间将该计数存储在某个位置.一个很好的地方是在cookie中. Flask包含了会话API ,以简化此操作.

You need to store that count somewhere between calls. A good place to do that is in a cookie; Flask includes the sessions API to make this simpler.

这篇关于计算Flask中的登录尝试次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆