私のDjangoプロジェクトには、フィールド( "title")を持つフォームがあります。このフィールドでは、ユーザーはロシア語で書く必要がありますが、誤ってラテン文字を書くことができます。私はそれらを変更したい。私はPYTHON 2.7を使用します。次のコードはエラーを発生させます。このエラーを修正するには?私はそれが誤り上げるだけform.cleaned_data['title'].translate(maketrans(eng, rus))
を使用する場合テキスト内の文字を他の文字に変更する方法| Python 2.7?
Traceback (most recent call last):
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/edit.py", line 217, in post
return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/edit.py", line 183, in post
return self.form_valid(form)
File "/home/nurzhan/dashboard.kase.kz/static_pages/views.py", line 54, in form_valid
form.cleaned_data['title'].translate(maketrans(dict(zip(eng, rus))))
TypeError: maketrans() takes exactly 2 arguments (1 given)
:
from string import maketrans
eng = 'ETOPAHKXCBMetopahkxcbm' # English letters
rus = 'ЕТОРАНКХСВМеторанкхсвм' # Russian letters
def form_valid(self, form):
form.cleaned_data['title'].translate(maketrans(dict(zip(eng, rus))))
form.save()
ERROR
Internal Server Error: /static_page/create/
Traceback (most recent call last):
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/srv/envs/py27/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/edit.py", line 217, in post
return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/srv/envs/py27/lib/python2.7/site-packages/django/views/generic/edit.py", line 183, in post
return self.form_valid(form)
File "/home/nurzhan/dashboard.kase.kz/static_pages/views.py", line 54, in form_valid
form.cleaned_data['title'].translate(maketrans(eng, rus))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-21: ordinal not in range(128)
自分の関数定義から自分自身を削除します。 –
「maketrans()のヒントは2つの引数(1つの引数)をとります」 - [string.maketrans(from、to)]を参照してください(https://docs.python.org/2/library/string.html#string UnicodeEncodeError: 'ascii'コーデックは、0-21の位置にある文字をエンコードできません:ordinal(.maketrans)。 –
'form.cleaned_data ['title']。translate(maketrans(eng、rus)範囲(128)ではない '。あなたはそれについて何か考えていますか? –