最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to work with time picker widget in Django template? - Stack Overflow

programmeradmin3浏览0评论

I am working on a Django based Application, and i stuck in some place where i want to save a time given by the user and the no of days (Monday-Sunday) in my database.

user will choose a time from Time picker widget or any other way, but it return into the 07 : 30 format.

User will also choose day's from any picker or any other way (user can select multiple days), and it will save into the database.

I tried with the Django-Model field by putting :

timestamp = Models.DatetimeField()

and pass in Django-template using the Django model form, but it shows a text field (which doesn't make any sense).

If, Anyhow it would be possible to get the time in 13:45 format and mon,tue,wed format, then i can also save it into CharField() in database.

I am not able to figure out the solution of above two situation. What are the best option to do it?

Should it be possible with the Java script? (but i am not professional in java script)

I am working on a Django based Application, and i stuck in some place where i want to save a time given by the user and the no of days (Monday-Sunday) in my database.

user will choose a time from Time picker widget or any other way, but it return into the 07 : 30 format.

User will also choose day's from any picker or any other way (user can select multiple days), and it will save into the database.

I tried with the Django-Model field by putting :

timestamp = Models.DatetimeField()

and pass in Django-template using the Django model form, but it shows a text field (which doesn't make any sense).

If, Anyhow it would be possible to get the time in 13:45 format and mon,tue,wed format, then i can also save it into CharField() in database.

I am not able to figure out the solution of above two situation. What are the best option to do it?

Should it be possible with the Java script? (but i am not professional in java script)

Share Improve this question asked Aug 14, 2012 at 19:55 Amit PalAmit Pal 11.1k31 gold badges85 silver badges169 bronze badges 2
  • looking at the django docs for the datetime widget it will be a textbox. docs.djangoproject./en/1.4/ref/forms/widgets/#datetimeinput however you might research modernizer along with some html5 stuff to get datetime picker. The DatetimeField is just a class to allow some validation of data and to have an object to do fun programming stuff. – Buddy Lindsey Commented Aug 14, 2012 at 20:06
  • What about choose from any Time picker, and this picker is built in java script – Amit Pal Commented Aug 14, 2012 at 20:09
Add a ment  | 

1 Answer 1

Reset to default 2

To use the widget, you should indicate in the form class for your model what the widget is:

from django import forms

class MyModelForm(forms.ModelForm):
    timestamp = forms.DateField(widget=forms.DateInput(attrs={'class':'timepicker'}))

If you're using the timestamp property in the model, it operates like a basic python datetime class, with many of the same attributes you would find there. If the format of the timestamp is what's bothering you, you might look at filters that django has for displaying time and datetime fields: https://docs.djangoproject./en/dev/ref/templates/builtins/#date

I'm not certain why you would need to save the timesstamp in a specific format; django's datetime field is pretty robust and easy to work with.

发布评论

评论列表(0)

  1. 暂无评论