Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Using strftime() in datetime fields [RSS Feed]

#1 Nov. 11, 2005 21:06:53

Pedro F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using strftime() in datetime fields


Please, can someone tell me if I'm doing anything wrong?

Basically, the code bellow is used in a view and generates the following error:
---
text = "blah blah %s" % (object.datetimefield.strftime('%d-%m-%Y'))
---

AttributeError: 'str' object has no attribute 'strftime'

'str' ? When I go to the prompt the datetimefield is a python datetime type...

--
Pedro Furtado

Offline

#2 Nov. 11, 2005 21:44:59

Eric W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using strftime() in datetime fields


On Friday 11 November 2005 12:41, Pedro Furtado wrote:
> object.datetimefield.strftime('%d-%m-%Y')

> AttributeError: 'str' object has no attribute 'strftime'

Turn it into a date, datetime or time object first, then format it.
Like the traceback says, strings don't have strftime
attributes/methods.
-E

Offline

#3 Nov. 12, 2005 01:46:48

Pedro F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using strftime() in datetime fields


> Turn it into a date, datetime or time object first, then format it.
> Like the traceback says, strings don't have strftime
> attributes/methods.

But it already is a datetime object. The "object" I stated is an
object of a model class, which contains a datetimefield. Am I clear
now?

The code runs smoothly in Python prompt but not in views.

Pedro

--
Pedro Furtado
Juiz de Fora - MG

Offline

#4 Nov. 12, 2005 02:16:57

Eric W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using strftime() in datetime fields


On Friday 11 November 2005 17:46, Pedro Furtado wrote:
> > Turn it into a date, datetime or time object first, then format
> > it. Like the traceback says, strings don't have strftime
> > attributes/methods.
>
> But it already is a datetime object.

Not according to the traceback you posted.
-E

Offline

#5 Nov. 12, 2005 02:56:25

Jeffrey E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using strftime() in datetime fields


One trick I've found that helps in debugging is to throw out allassumptions, and to work backwards from a traceback while doing so.In your case, Python is telling you that what you *think* and what*really ought to be* a DateTime object is instead a string. So,double-check--print out the value of type(object.datetimefield)sometime just prior to where the traceback hits. Do a dir() on it.Stuff like that. It seems dumb and simple but it often helps.The other thing is to look at the differences between the twoenvironments, or between what you're doing in the prompt and what'shappening in the view. Again, throw out assumptions and double-check*everything* surrounding the problem object.One thing that might help us target specific things going wrong is ifyou pasted the model class definition, and what specifically you'redoing in the prompt to try and simulate the problematic view code.Regards,
Jeff

On Nov 11, 2005, at 8:46 PM, Pedro Furtado wrote:Turn it into a date, datetime or time object first, then format it.
Like the traceback says, strings don't have strftime
attributes/methods.But it already is a datetime object. The "object" I stated is an
object of a model class, which contains a datetimefield. Am I clear
now?

The code runs smoothly in Python prompt but not in views.

Pedro

--
Pedro Furtado
Juiz de Fora - MG--
Jeffrey E. Forcier
Junior Developer, Research and Development
Stroz Friedberg, LLC
15 Maiden Lane, 12th Floor
New York, NY 10038
212-981-6540 212-981-6546http://www.strozllc.comThis message is for the named person's use only. It may contain
confidential, proprietary or legally privileged information. No right to
confidential or privileged treatment of this message is waived or lost
by any error in transmission. If you have received this message in
error, please immediately notify the sender by e-mail or by telephone at
212.981.6540, delete the message and all copies from your system and
destroy any hard copies. You must not, directly or indirectly, use,
disclose, distribute, print or copy any part of this message if you are
not the intended recipient.

Offline

#6 Nov. 12, 2005 21:29:09

Eric W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Using strftime() in datetime fields


On Friday 11 November 2005 06:56 pm, Jeffrey E. Forcier wrote:
> One trick I've found that helps in debugging is to throw out all  
> assumptions, and to work backwards from a traceback while doing so.
>
> In your case, Python is telling you that what you *think* and what  
> *really ought to be* a DateTime object is instead a string. So,  
> double-check--print out the value of type(object.datetimefield)  
> sometime just prior to where the traceback hits. Do a dir() on it.  
> Stuff like that. It seems dumb and simple but it often helps.
>
> The other thing is to look at the differences between the two  
> environments, or between what you're doing in the prompt and what's  
> happening in the view. Again, throw out assumptions and double-check  
> *everything* surrounding the problem object.
>
> One thing that might help us target specific things going wrong is if  
> you pasted the model class definition, and what specifically you're  
> doing in the prompt to try and simulate the problematic view code.
>
> Regards,
> Jeff

Well said, Jeff. Thanks for taking the time to give Pedro related tips. I
want to help, but am very busy lately so my responses, when I have time to
give them are terse (hopefully, consise as well).
Best,
Eric.

Offline

Board footer

Moderator control

Enjoy the 20th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support