On May 24, 2016 3:32 PM, "Larry Martell" <larry.mart...@gmail.com> wrote:
>
> I have 1 page in my app where I want to override the app's date
> format. I tried assigning to settings.DATE_FORMAT in the view but that

Django warns that settings should not be modified at runtime, which is
probably why it had no effect:

https://docs.djangoproject.com/en/1.9/topics/settings/#altering-settings-at-runtime

> had no effect. I know I can format the date in the template, but
> without making a lot of changes, I don't know what fields are dates. I
> thought perhaps the date filter would just pass through non-dates
> unchanged, but that's not what it did.
>
> How can I override the date format just for 1 view or how can I tell
> in the template if a field is date or not?

I would use one of a couple options:

- Examine the data within the view, and modify those fields with the
correct date format before passing your data off to the context to be
rendered. At that point you still have the full complement of Python tools
at your disposal without the drag of the template processor nuances and
slower processing time.

- Create a custom filter that acts similar to the date filter that Django
provides, but give it more intelligence to detect what values should be
modified and what format your dates should use. Run it against all of your
fields. If the field is not a date, then return the field data unscathed.

Hopefully your 'dates' are true date or datetime objects, which would be
easy to detect and format. If they are strings, obviously you'll need to be
able to determine whether or not formatting is necessary by analyzing the
string contents. If you can easily detect a date field within your custom
filter, you can probably take advantage of the Django date filter directly
since it is just another function to save yourself writing code to format
the dates once the target fields are identified.

If it were me in a one-off exception page like you stated, I'd just mangle
the data in the view and move on with life. It'll likely be faster than
having the template processor handle it for any significant number of
fields. If there is a chance you'll need this formatting magic later in
another page, the template filter may be the way to go.

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWnPt7urkjTpCMeBiSVuDxOseJNM7S-oipzre%2B3jPer1g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to