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?