Use custom Django filter in Google App Engine to solve time zone problems

Source: Internet
Author: User
Tags timedelta

The previous solution was to convert the time in the front-end interface JS, which was not very good and found that there was an incorrect display in IE. Therefore, we plan to use the Django custom filter method. There are some precautions that may be mistaken if you are not careful, leading to page errors.

1. Add the templatetags directory.

Note that:

    1. The directory name must be templatetags so that Django can find it.
    2. Location of the Directory templatetags.
      For example, my app directory is ncblog (App. yaml directory). In the ncblog directory, there is also a directory named Nancy. At this time, I created a directory named templatetags under the Nancy directory and put an empty _ init __. py. The directory structure is as follows:
      Ncblog
      ---- Nancy
      ---- Blog
      ---- Templatetags
2. Set installed_apps in settings. py

To locate the templatetags, we need to add a path to nstalled_apps in settings. py, for example, add:

Installed_apps = (
' Django. contrib. auth ' ,
' Django. contrib. contenttypes ' ,
' Django. contrib. Sites ' ,
' Nancy ' , # I added this
) 3. compile our filter.

Create a file with any file name in the templatetags directory, such as filters. py. Now we want to convert the time zone into a filter. ThereforeCodeAs follows:

From Django Import Template
From Datetime Import Timedelta
Register = Template. Library ()
Def Timezone (value, offset ):
Return Value + Timedelta (hours = Offset)
Register. Filter (timezone) 4. Add {% load filters %} at the beginning of the template file, and then you can use it like this: {Log. Date | Timezone: 8 | Date: " Y-m-d h: I: S " }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.