1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Django】pytzのtimezone 名一覧のプルダウンメニューを作る

Last updated at Posted at 2019-06-05

#環境
Django 1.11
python 3.6.6

#こういうやつ

Djangoでタイムゾーン名を入力させるフォームを作りたかったので,
pytzのtimezone一覧のプルダウンメニューを作成した.
スクリーンショット 2019-06-05 16.53.35.png

#手順

timezone一覧のリストをつくって, viewからtemplateに投げてあげればOK.

services.py
import pytz

def timezoneList():
    timezone_list = pytz.common_timezones
    return timezone_list
views.py

from XXX.services import *

class TestPage(LoginRequiredMixin,TemplateView): #()内はよしなに
    def get(self,request):
    ...
        timezones = timezoneList()

        context = {
        ...,
        'timezones':timezones,
        }

        render (request,'test.html',context)

< select >タグ内に以下のように書いた.

test.html

{% for timezone in timezones %}
<option value ='{{ timezone }}' > {{ timezone }} </option>
{% endfor %}

更新してあげると出来てた. やったね!

#補足

ChoiceFieldを使う方法(こっちの方が多分楽)
https://stackoverflow.com/questions/10578440/how-to-add-drop-down-list-for-timezone-using-python-tz-at-user-registration-page

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?