Django Timezones

Timezone selection in Django

The pure magic that happens once you start a project in Django is one to behold. A lot has been handled for us, leaving one with ample time to focus on writing our app rapidly. Four files are generated which include: the settings.py, urls.py,wsgi.py, and manage.py.

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source. ~Django Documentation

Settings.py

Let's take a closer look at the settings page. This is where Django stores all the configuration for an app by default. In the absence of an environment variable, this is where Django looks for it by default. It is here where we:

  1. Register our apps
  2. Configure our databases
  3. Provide routing for the static files, media, and templates in our projects
  4. Change timezones

Fun Activity 😊

Let us create a scenario whereby we want to update our timezone from the default "UTC" to our current timezone. OK Yes, you can ask "uncle Google", check StackOverflow and your answer will be provided on a silver platter, it will only be a matter of copying and pasting.

But let us give ourselves a challenge and check our timezone from the pytz package using the python shell and basic python knowledge.

Check various timezones from the pytz python package

You will need the terminal/cmd, basic python knowledge, and teachability

Open your cmd/terminal and cd into your Django project and activate your virtual environment. Run the following commands:

python manage.py shell

The python shell is also known as the Python Interactive Shell. It is used to execute a single Python command and return a result.

shell.JPG

Import the package called pytz and then call the module called pytz.all_timezones_set which contains all timezones of the world in the form of a python set

In [1]: import pytz
In [2]: pytz.all_timezones_set

Captureset.JPG

Python Set Manipulation

We can choose to do it in many ways as there are many ways to kill a rat. Let us look at two methods:

  1. Using the set data type as it is
  2. Using a List data type

Let us Dive in!! 👌

1. Using the set data type (as is)

Using a list comprehension, loop through the pytz.all_timezones_set and assign each item to a variable x, using the if statement , check if a particular world/country name exists, in our case, we will check for Africa, and return all variable x's that contain the word specified

In [3]: new_timezone_set =[ x for x in pytz.all_timezones_set if 'Africa' in x]  
In [4]: print(new_timezone_set)

As shown below we are now able to get a specified timezone name from the set using a list comprehension, for loop and if statement.

Capture2.JPG

2. Using a List data type

First, convert the set to a list data type using: list() and assign it to a variable. This changes the set curly brackets {..} to the list square brackets [...]

  new_list =list(pytz.all_timezones_set)
  print(new_list)

list.JPG

Using the filter() and lambda function, check for a specified timezone name

  In [9]: specific_timezone = filter(lambda a:'Africa' in a,new_list)
  In [11]: print(list(specific_timezone))

filter.JPG

Now, we can easily choose a specific timezone effortlessly and place it in your settings file as the set is now narrowed to specific timezones.

I hope, you enjoy the tutorial.

As always, stay safe and code on

Happy coding Pythonistas

No Comments Yet