期待内容
html
1・北海道 2・青森県
これまでにしたこと
settings.pyに定義した都道府県コードと対応する名称を作り、templates側で呼び出せるように
mysite/myapp/context_processors.pyを用意。
python3
1# settings.py 2TEMPLATES = [ 3 { 4 # 一部省略 5 'OPTIONS': { 6 'context_processors': [ 7 'myapp.context_processors.pref_choices', # <---追加 8 ], 9 }, 10 }, 11] 12PREF_CHOICES = ( 13('01', '北海道'), 14('02', '青森県'), 15) 16 17# mysite/myapp/context_processors.py 18from django.conf import settings 19 20def pref_choices(request): 21 return {'PREF_CHOICES': settings.PREF_CHOICES} 22
html
1<!-- ・北海道とは表示されないのは無視 --> 2<ul class="list-group" style="max-width: 70%;"> 3 <ul class="list-group"> 4 {% for pref in PREF_CHOICES %} 5 <li class="list-group-item"> 6 {{ pref }} 7 </li> 8 {% endfor %} 9 </ul> 10</ul>
とすると以下の様に表示
('01', '北海道') ('02', '青森県')
上記を期待し以下のように記述
html
1<ul class="list-group" style="max-width: 70%;"> 2 <ul class="list-group"> 3 {% for pref in PREF_CHOICES %} 4 <li class="list-group-item"> 5 {{ pref[1] }} 6 </li> 7 {% endfor %} 8 </ul> 9</ul>
以下は表示されたTemplateSyntaxError
なぜsyntaxErrなのかがわからないです。
ご教示いただけますでしょうか。
Could not parse the remainder: '[1]' from 'pref[1]' Request Method: POST Request URL: http://192.168.33.20:8000/comments/search/ Django Version: 2.1.5 Exception Type: TemplateSyntaxError Exception Value: Could not parse the remainder: '[1]' from 'pref[1]' Exception Location: /home/vagrant/venv/3.7.2/lib/python3.7/site-packages/django/template/base.py in __init__, line 663 Python Executable: /home/vagrant/venv/3.7.2/bin/python Python Version: 3.7.2 Python Path: ['/home/vagrant/venv/3.7.2/xxxxx', '/home/vagrant/venv/3.7.2/lib/python37.zip', '/home/vagrant/venv/3.7.2/lib/python3.7', '/home/vagrant/venv/3.7.2/lib/python3.7/lib-dynload', '/home/vagrant/.pyenv/versions/3.7.2/lib/python3.7', '/home/vagrant/venv/3.7.2/lib/python3.7/site-packages']
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/03/10 12:59