0

The default value of the form input should be the current time. The user should can select to provide a time in the future and than a normal datetime field should be shown. Can this be realized without javascript and how?

  • 1
    Do you mean, dates in the past should not show up in calendar? – Chirdeep Tomar Feb 20 '17 at 18:26
  • What happens otherwise?. I'm thinking of django validators and python's `property()` function, perhaps if you add more details to your question then we can give you a better answer – chachan Feb 20 '17 at 19:03
  • Is this what you're looking for? http://stackoverflow.com/questions/4941974/django-how-to-set-datefield-to-only-accept-today-future-dates – themanatuf Feb 20 '17 at 19:24
  • @themanatuf That seems to be a part of the solution. But it does not cover the case when the user does not want to provide a publication date in the future and release the question immediately. –  Feb 20 '17 at 20:39

2 Answers2

0

You can leave the input empty and change it to the current time on the backend (or maybe set it to the current time at the moment of rendering template and interpret past times as "now"). But the most you can do without JavaScript is adding a comment that empty input means current time. If you want to make some kind of fancy checkbox for current time and maybe hide the datetime input, you should do it with JavaScript

Domen Blenkuš
  • 2,182
  • 1
  • 21
  • 29
0

You can try to use <input name="date_input" type="date" id="date_input"> in your template.

And do views do :

from datetime import datetime
# get a string from a POST like "10/2/2018"
date_get = request.POST.get("date_input")

# format of date/time strings; assuming dd/mm/yyyy
date_format = "%d/%m/%Y"

# create datetime objects from the strings
date = datetime.strptime(date_get, date_format)

# Check if we have a datetime
print(date)
datetime.datetime(2018, 2, 10, 0, 0)
Pyvonix
  • 757
  • 9
  • 22