How to set default date in SelectDateWidget in django - django

I am using SelectDateWidget widget for entering date in the form field. But I want it to show the current date by default. How can I do that?
model.py
bdate = models.DateField(default=datetime.date.today())
This is giving error. can anyone tell the correct way to do that?
Also, my in template
{{ form.bdate }}
when I use the above mentioned line in my template it displays like this -- -- --
but I want something like this month date year. how can i do this?
My form is:
widgets = {
'bdate' : SelectDateWidget(),
}

Using Django 1.3 the following worked for me:
join_date = forms.DateField(widget=SelectDateWidget(), label='Joining Date', initial=timezone.now())
The keyword is initial not default.

There was similar problem here
Try to use default=datetime.date.today or auto_now_add=True

Related

Django 1.11 - ModelForm: change default form widget for DateTimeField

I need some help in rendering a datetime picker in my form insead of the default text field that is displayed. I am using Django 1.11 and have followed the recent solution posted here: Django 1.11 - forms.Models: change default form widget for DateTimeField however
I receive errors when using the same code.
The first error I receive is: cannot import name 'widget'. I can pass this error by importing 'widgets' instead. Has this been renamed?
The second error I receive after renaming to widgets is NameError: name 'forms' is not defined. I can pass this error by changing the code to:
Class DateInput(widgets.DateInput):
Is this the correct treatment for this error?
The third error I receive is: NameError: name 'Date_Input' is not defined I can pass this error by changing the final piece of code (removing underscore in Date_Input) above to:
widgets = {
'missing_date': DateInput()
}
After these changes, I no longer get any errors however the date field in my form is still rendering as a text field and not as a date picker.
Can anyone shed any further light on the solution above and why it possibly isn't working for me?
Additionally I would like to modify the solution mentioned in the link above to render a datetime picker not just date picker, but I first wanted to test the functionality using the code from the previous post solution. Thanks!
I ended up resolving this by splitting the datetime into seperate fields of date and time and using the following code in forms.py:
class TimeInput(forms.TimeInput):
input_type = 'time'
class DateInput(forms.DateInput):
input_type = 'date'
and within my ModelForm Class:
widgets = {
'start_date': DateInput(),
'start_time': TimeInput(),
'end_date': DateInput(),
'end_time': TimeInput(),
}

Change Django date time field auto_now_add format

I want to know if there is any way to change Django dateTimeField format, I want to set the time using auto_now_add attribute but I can't get it to save it with the format I want. I know that there is a way suing models.DateField but I need to use auto_now_add. Thanks
I don't know if it would help any one in the future but here's the answer :
I had to change the field in my model to created = models.DateField(blank=False, auto_now_add=True) and create a new field in the serializer class like this (on top of the Meta class) created = serializers.DateTimeField(read_only=True), that worked quite fine.

Django change the way date field is displayed in django admin

That's it i want to change the way its displayed cause it says "noon" or "midnight" and i want i to display the exact hour
I've got this code on the field
date = models.DateTimeField(auto_now_add=True, default=datetime.date.today())
You have 3 options.
Override DATETIME_FORMAT, read here: datetime format
Use a form for your model and in the date attribute have some like this(
in input format, put whatrever you want):
date = forms.DateTimeField(input_format="%b %d %Y %I:%M%p")
Or just change the way you render your datetime field in the template:
{{ your_model.date_field|date:'Y-m-d H:i' }}

Django DateTimeField

I have a field in my model
published_on = models.DateTimeField()
on a Django template page and I want to show only the date instead of date along with time. Any idea how to truncate the time form date in the django model form?
Thanks in advance.
Use the date filter in your template, for instance:
{{ form.published_on|date:"D d M Y" }}
Or just:
{{ form.published_on|date }}
You can customize the output the way you want, or use the locale default. See this link for details.
You could try to use SplitDateTimeWidget to represent the field in two widgets (https://docs.djangoproject.com/en/dev/ref/forms/widgets/#splitdatetimewidget). Then, for example, the time field could be hidden with CSS. This method is particularly useful if you need to preserve the actual time value, and just allow user to change the date.

Django TimeField Model without seconds

Greetings,
I am trying to implement a TimeField model which only consists of HH:MM (ie 16:46) format, I know it is possible to format a regular Python time object but I am lost about how to manage this with Django.
Cheers
Django widget can be used to achieve this easily.
from django import forms
class timeSlotForm(forms.Form):
from_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
DateTime fields will always store also seconds; however, you can easily tell the template to just show the hours and minute, with the time filter:
{{ value|time:"H:M" }}
where "value" is the variable containing the datetime field.
Of course, you can also resort to other tricks, like cutting out the seconds from the field while saving; it would require just a small change to the code in the view handling the form, to do something like this:
if form.is_valid():
instance = form.save(commit=False)
instance.nosecs = instance.nosecs.strptime(instance.nosecs.strftime("%H:%M"), "%H:%M")
instance.save()
(note: this is an ugly and untested code, just to give the idea!)
Finally, you should note that the admin will still display the seconds in the field.
It should not be a big concern, though, because admin should be only used by a kind of users that can be instructed not to use that part of the field.
In case you want to patch also the admin, you can still assign your own widget to the form, and thus having the admin using it. Of course, this would mean a significant additional effort.
So I think the proposed and accepted solution is not optimal because with:
datetime.widget = forms.SplitDateTimeWidget(time_format=('%H:%M'))
For a SplitDateTimeField in my case but for you only change it to TimeWidget.
Hope it helps other people too.
TimeField model
in Template
Is displayed
{{ value|time:"H:i" }}
Is not displayed
{{ value|time:"H:M" }}
Django 1.4.1
For a ModelForm, you can easily add a widget like this, to avoid the seconds being shown (just show hh:mm):
class MyCreateForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ('time_in', 'time_out', )
widgets = {
'time_in': forms.TimeInput(format='%H:%M'),
'time_out': forms.TimeInput(format='%H:%M'),
}
You can at least modify the output in the __str__ method on the model by using datetime.time.isoformat(timespec='minutes'), like this:
def __str__(self):
return self.value.isoformat(timespec='minutes')
Now the value is showing as HH:MM in admin pages.
On Django 1.9 the following format should work:
{{ yourData.value|time:"H:i" }}
Django has a whole set of template tags and filters.
Django 1.9 documentation on this is:
https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#time