Python / Flask:如何判断用户在页面上花了多长时间? (数据输入/时间日志应用程序) [英] Python/Flask: How to tell how long a user spends on a page? (Data entry/time log app)

查看:199
本文介绍了Python / Flask:如何判断用户在页面上花了多长时间? (数据输入/时间日志应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到用户使用Javascript在页面上花了多长时间的答案,但是我对JS的了解却很少(很少将JS集成到Python / Flask框架中)。我的目标是创建一个用户可以输入数据的Web应用程序,同时记录完成任务所花的时间(包括持续时间和白天的时间)。

对于大多数功能,我正在使用Python,Flask,WTForms和SQLAlchemy。从我的views.py文件,我有这个配置:

  @ app.route('/ logpage',methods = [' ():
form = LogForm()
$ b $如果form.validate_on_submit():
entry = LogData(sessionid = form.sessionid.data,user_id = current_user.get_id(),endtime = datetime.utcnow())
db.session.add(entry)
db.session.commit ()

return redirect(url_for('home'))

return render_template('logpage.html',form = form)


我试图找出一种方法来将当前日期时间写入SQL入口/行,并在表单验证时输入到同一行,但是我没有意识到如何写入同一行两次。也许与主键和链接他们的东西?我会继续调查这一点。



我想要一个方法来保存这个页面加载的日期时间,所以可以和表单提交的时间进行比较。有没有一种简单的方法来完成这个没有JavaScript?

解决方案

您可以使用Flask会话来跟踪开始时间。它是在浏览器cookies之上实现的。



http://flask.pocoo.org/docs/0.12/quickstart/#sessions



您需要为应用程序(如快速入门示例中所示),然后可以使用会话对象作为用户特定信息的关键值存储区。



对于您的特定用例,可能是这样的:

  @ app.route('/ ()方法= ['GET','POST'])
@login_required
def logpage():
form = LogForm()

if form。 validate_on_submit():
entry = LogData(sessionid = form.sessionid.data,user_id = current_user.get_id(),
starttime = session.pop('start_time',None),endtime = datetime.utcnow ())
db.session.add(entry)
db.session.commit()

return redirect(url_for ('home'))

session ['start_time'] = datetime.utcnow()

return render_template('logpage.html',form = form)

pageload = datetime.utcnow()因为:


  • 这个变量对于函数的作用域是局部的,在函数完成后不会保留 li>
  • 即使变量不在函数调用的范围内,也会为GET和POST调用同一个函数,所以当用户发表表单时, li>


还有一点需要注意的是,您不能相信用户使用cookie或允许使用JavaScript,所以您应该考虑你的程序如何处理数据库中的空启动时间。


I have seen answers to see how long a user spends on a page using Javascript, but my knowledge of JS is lacking (much less integrating JS into my Python/Flask framework).

My objective is to create a web application that users can enter data into, while the time spent (both duration and what time during the day) doing the task is recorded.

I am using Python, Flask, WTForms, and SQLAlchemy for most of the functionality. From my views.py file, I have this configured:

@app.route('/logpage', methods=['GET', 'POST'])
@login_required
def logpage():
    form = LogForm()

    if form.validate_on_submit():
        entry = LogData(sessionid=form.sessionid.data, user_id=current_user.get_id(), endtime=datetime.utcnow())
        db.session.add(entry)
        db.session.commit()

        return redirect(url_for('home'))

    return render_template('logpage.html', form=form)

When the user hits submit, the Session ID, the user's ID, and the endtime is recorded to a PostgreSQL server. I tried setting pageload = datetime.utcnow() before the form validation in hopes it would store the time the page loaded, but it seems to save the same time as end time.

I was trying to figure out a way to write the current date time to the SQL entry/row and entering to the same row when the form validates, but I am unaware of how to write to the same row twice. Perhaps something with the primary key and linking them? I will continue to investigate this.

I would like a way to save the datetime this page loads so it can be compared to the time the form is submitted. Is there a straightforward way to accomplish this without javascript?

解决方案

You can use a Flask session to track the start time. It's implemented on top of browser cookies.

http://flask.pocoo.org/docs/0.12/quickstart/#sessions

You need to implement a secret key for the app (as shown in the quickstart example), and then you can use the session object as a key value store for user-specific information.

For your particular use case, it might be something like:

@app.route('/logpage', methods=['GET', 'POST'])
@login_required
def logpage():
    form = LogForm()

    if form.validate_on_submit():
        entry = LogData(sessionid=form.sessionid.data, user_id=current_user.get_id(), 
                        starttime=session.pop('start_time', None), endtime=datetime.utcnow())
        db.session.add(entry)
        db.session.commit()

        return redirect(url_for('home'))

    session['start_time'] = datetime.utcnow()

    return render_template('logpage.html', form=form)

pageload = datetime.utcnow() before the form validation didn't work because:

  • this variable would be local to the scope of the function and wouldn't persist after the function completes
  • even if the variable weren't local to the scope of the function call, the same function is being called for both GET and POST, so it would be overridden when the user posts the form

One more thing to be aware of is that you can't trust the user to be using cookies or to allow JavaScript, so you should consider how your program would handle null start times in the database.

这篇关于Python / Flask:如何判断用户在页面上花了多长时间? (数据输入/时间日志应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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