LoginSignup
0
0

More than 5 years have passed since last update.

Python3+ Djangoでset_cookieのvalueに日本語を入れたい!

Last updated at Posted at 2017-03-27

日本語をそのまま入れると、Exceptionを吐いてしまうので、さあどうすっぺかと考えた。

バージョン

$ python3 --version
Python 3.5.2
$ python3
>>> import django
>>> django.get_version()
'1.10.5'

解決までの道筋

1. urlencodeを使う

  • 成功:django.utils.http.urlencode(['愛す'], False)
  • 成功:django.utils.http.urlencode(['アイ'], False)
  • 失敗:django.utils.http.urlencode(['バルス'], False)
  ・
  ・
  ・
  File "/usr/local/src/django/django/utils/http.py", line 104, in urlencode
    for k, v in query],
  File "/usr/local/src/django/django/utils/http.py", line 104, in <listcomp>
    for k, v in query],
ValueError: too many values to unpack (expected 2)

2. enumerate+urlencodeを使う

  • 成功?:django.utils.http.urlencode(enumerate(['バルス']), False)
    • "0=%E3%83%90%E3%83%AB%E3%82%B9"になってしまう。"0="が邪魔。

3. urlquoteを使う

  • 成功!:django.utils.http.urlquote('バルス', '')
    • %E3%83%90%E3%83%AB%E3%82%B9

でもちょっと本来の使い方ではない気がする。。。

0
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
0
0