So I'm a beginner in Django, and recently came up with a question, regarding datetime.
So I'm trying to make a blog-like page. And among the input fields, including title and contents, I want a datetime field as well. However, there is an additional feature that I want to create, which is -- if the user clicks a checkbox right next to the datetime input field, the datetime will automatically change into the CURRENT date and time. So the input field is replaced with the current date and time.
I have no idea on how to create the feature.
I would very much appreciate your help :)
Asked
Active
Viewed 51 times
0

Ryan Oh
- 597
- 5
- 21
-
Does this answer your question? [Django auto\_now and auto\_now\_add](https://stackoverflow.com/questions/1737017/django-auto-now-and-auto-now-add) – Pavel Vergeev Mar 22 '20 at 12:22
-
Well, it partly does. So I'm not sure if that answer in your link is about 'replacing the datetime field with the current date and time when a checkbox next to it is clicked.' Sorry I'm a beginner and have trouble applying them. – Ryan Oh Mar 22 '20 at 12:39
-
Cuz I want to let the users change the date and time of the creation of post, unless they want to set it with current date and time. – Ryan Oh Mar 22 '20 at 12:41
1 Answers
1
It will be better if you make this behavior to be set automatically to the time of creating the post for the first time, it will not be triggered if you modify the post:
created_at = models.DateTimeField(auto_now_add=True)
If you want to set it to the current time when you modify the post:
modified_at = models.DateTimeField(auto_now=True)

Ambitions
- 2,369
- 3
- 13
- 24
-
Thanks for the reply. However I want the users to freely choose the date and time, unless they want to set it as current. So I was thinking of having datetime field in the beginning, but replace it into the current date and time when a checkbox is clicked right next to it. – Ryan Oh Mar 22 '20 at 12:42