Friday, February 25, 2011

Re: how to get the time from date-based generic views

OHHH this is really impressive. I didn't really think of that.

Thanks! Now I have another concept loaded under my belt. Hhaha

THank!

On Feb 26, 12:18 am, Russell Keith-Magee <russ...@keith-magee.com>
wrote:
> On Sat, Feb 26, 2011 at 1:09 PM, John Yeukhon Wong
>
> <gokoproj...@gmail.com> wrote:
> > Suppose we have a django page using
> > django.views.generic.date_based.object_detail (or even archive_index..
> > actually doesn't really matter...)
>
> > In the model class I saved the datetime.datetime.now which suppose to
> > include the day, month, year, and time.
>
> > But I have no idea how to access the time part when i am using this
> > generic views
>
> > What I want is Feb 26, 2011,  01:12:04 AM
> > I can get the calendar part by {{ object.pub_date}}
>
> > How do I get the time part?
>
> The answer doesn't really have anything to do with class based views
> -- it's a templating issue.
>
> Your model has a field that contains a datetime field -- a single
> field, called pub_date, that contains a date component and a time
> component. When you output {{ object.pub_date }}, it's outputting the
> entire date-time content. If you want to only show the time component,
> you have two options:
>
> 2) Call the time() method on the pub_date attribute.
>
> {{ object.pub_date.time }}
>
> This is possible because all datetime objects (whichs is what
> object.pub_date is returning) have a built-in method called time()
> that returns the time component, and Django's template engine will
> traverse (and invoke) built-in methods as part of the dot-notation
> syntax.
>
> 1) Use the |date filter to only print time-related components.
>
> {{ object.pub_date|date:"P" }}
>
> would output "1:12 AM". This is ultimately the most flexible approach,
> because you can determine exactly how dates are displayed. See the
> docs on the date filter [1] for other formatting options.
>
> [1]http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#date
>
> Yours
> Russ Magee %-)

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment