Re: Date and Times
thanks koenb i'll try that this evening. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Date and Times
H René, This is what I use on all my forms (I use my own modelform subclass to insert these for date fields, but that is not relevant): as formfield: class DateFormField(forms.DateField): def __init__(self, input_formats=None, *args, **kwargs): super(DateFormField, self).__init__(*args, **kwargs) self.input_formats = input_formats or settings.DATE_INPUT_FORMATS def clean(self, value): super(forms.DateField, self).clean(value) if value in forms.fields.EMPTY_VALUES: return None if isinstance(value, datetime.datetime): return value.date() if isinstance(value, datetime.date): return value for format in self.input_formats: try: y, m, d = time.strptime(value, format)[:3] if y == 1900: y = datetime.datetime.now().year return datetime.date(y, m, d) except ValueError: continue raise forms.util.ValidationError(self.error_messages ['invalid']) and as widget: # like admin date widget with proper date formatting class DateWidget(forms.DateTimeInput): class Media: js = (settings.ADMIN_MEDIA_PREFIX + "js/calendar.js", settings.ADMIN_MEDIA_PREFIX + "js/admin/ DateTimeShortcuts.js") def __init__(self, attrs={}): super(DateWidget, self).__init__ (format=settings.DATE_OUTPUT_FORMAT, attrs={'class': 'vDateField', 'size': '10'}) and in settings: DATE_INPUT_FORMATS = ( '%d/%m/%Y', '%d/%m/%y', # '25/10/2006', '25/10/06' '%d/%m', # '25/10' '%Y-%m-%d', # '2006-10-25' ) DATE_OUTPUT_FORMAT = '%d/%m/%Y' This way the field accepts all my formats (also the format the calendar widget returns) and the output is rendered the way I want it. Notice that the custom clean is there because I want to accept dates like 18/02 without the year and use the current year for those. Hope this helps somebody, and would appreciate to hear if there are better ways, Koen --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Date and Times
> That was a whole 19 hours before you posted this! It's not as if it's > been a week with no answer. For a question like this, where even > understanding the problem requires wading through 40 lines of code, it > might well take a while before somebody has time and motivation to get > there. > > Regards, > malcolm I know this was very short but i can not belive i am the first one having this problem. Nearly every german application should have to deal with this. I am sorry for this but it was very important to know if it's solveable ... Although i got the code from the official django tickeing system, so i was pretty sure many peoples would have used the code before. But i'll take some patience next time. Guaranteed --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Date and Times
Would you say i am right? I would past this on http://code.djangoproject.com/ticket/3672 where i got the original code from --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Date and Times
Ok got this fixed. The problem comes if you enter invalid data. return dateformat.format(value, self.format) would throw an exception and the form would render to "" class DateFormattedTextInput(FormattedTextInput): "Renders formatted date." def __init__(self, format=None, attrs=None): super(DateFormattedTextInput, self).__init__(attrs) self.format = format or settings.DATE_FORMAT def format_value(self, value): if value: try: #here was the problem return dateformat.format(value, self.format) except: return value else: return value Solves this. If the value can not be formated the original one is returned. BTW: the problem only occured after calling form.isvalid(). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Date and Times
On Tue, 2009-02-17 at 07:28 -0800, ReneMarxis wrote: > Very sorry to bug you againe, but this is getting very critical > > Please can someone tell me how to format output of one DateField in a > way that form.is_valid() is not breaking? What do you mean by breaking? When I use the example code you pasted initially (http://dpaste.com/121354/ ), I see this: In [12]: f = forms.SchuljahreAddFormMain({'Datum_Von': '31.15.2009'}) In [13]: f.errors Out[13]: {'Datum_Von': [u'Enter a valid date.']} In [14]: print f Datum Von:Enter a valid date. The erroneous value is being included when the form is redisplayed. What are you seeing in a similar case? You haven't really described the error you are experiencing. > I allready posted this question some time before under > http://groups.google.com/group/django-users/browse_thread/thread/33b6fadb5db48525 > but got no result. That was a whole 19 hours before you posted this! It's not as if it's been a week with no answer. For a question like this, where even understanding the problem requires wading through 40 lines of code, it might well take a while before somebody has time and motivation to get there. Regards, malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Date and Times
What i basicly need: Have one imput format and the same output format for modeled and nonmodeld forms and all other functionalities should be present as default (e.g. is.valid()) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Date and Times
Very sorry to bug you againe, but this is getting very critical Please can someone tell me how to format output of one DateField in a way that form.is_valid() is not breaking? I allready posted this question some time before under http://groups.google.com/group/django-users/browse_thread/thread/33b6fadb5db48525 but got no result. This must be something, someone else allready steped over. I think every e.g. german application must deal with this.. I also need this for modeled forms. On those i can get the initial data showing up formated the right way, but after one failed .is_valid () the form is empty againe. Many thanks for your help. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---