[Django]-Getting time of form submission in Django

4👍

Two approaches:

First you could modify the view to create a field and add it to the form when you save the response. Something like:

if form.is_valid() :
    timestamp = datetime.now()
    ... save it, print it, whatever...

The better way is to handle it at model level, and just have a field for it.

created_at = models.DateTimeField(auto_now_add=True)

Now when you save the input to your DB, you’ve got creation time of when the save() method was called.

👤koblas

8👍

If you mean when you’re processing the form in the view, then why not just use datetime.datetime.now() at any point in the view? That’ll be milliseconds after the user pressed Submit

0👍

Http get/post operations don’t send the time to the server so as stevejalim mentions you can get the a timestamp in your server side code. Alternatively, you can pass an additional parameter representing the time the user clicked/submitted the page. You would set the parameter using javascript:

$('#form1').submit(function() {
    var input = $("<input>").attr("type", "hidden").attr("name", "timestamp").val(new Date().getTime());
    $('#form1').append($(input));
    return true;
}

Leave a comment