我将我的网站移植到django 1.9并且不知道如何正确解决此问题。
在我的表格中,我有来自django表格的常用EmailField。如果验证失败,它应该是关于它的消息(我将'form_errors': form.errors}
传递给上下文进行操作)。
但在那种情况下,django会返回
<{1>}DjangoUnicodeDecodeError:&#39; ascii&#39;编解码器不能解码字节0xd0 位置0:序数不在范围内(128)。你过去了 ()
django.core.validators
中有一个验证器
@deconstructible
class EmailValidator(object):
message = _('Enter a valid email address.')
...
如果我将消息更改为message = 'error'
,则一切正常。
所以,问题:如何在不编辑django文件的情况下修复此问题?
问题在于调用form.errors,即使我只想打印它,这个错误也会增加。 (print form.errors
)。另一个字段错误(例如,IntegerField,URLField)工作正常,此问题仅适用于EmailField。
在视图过程中现在看起来像这样:
from django.http import JsonResponse
...
if form.is_valid():
...
else:
return JsonResponse({'form_errors': form.errors})
最后一次追溯是:
File "/path/views.py", line 331, in custom_form_post
response = JsonResponse({'form_errors': form.errors})
File "/path/.env/local/lib/python2.7/site-packages/django/http/response.py", line 505, in __init__
data = json.dumps(data, cls=encoder, **json_dumps_params)
File "/usr/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/path/.env/lib/python2.7/_abcoll.py", line 581, in __iter__
v = self[i]
File "/path/.env/local/lib/python2.7/site-packages/django/forms/utils.py", line 146, in __getitem__
return list(error)[0]
File "/path/.env/local/lib/python2.7/site-packages/django/core/exceptions.py", line 165, in __iter__
yield force_text(message)
File "/path/.env/local/lib/python2.7/site-packages/django/utils/encoding.py", line 88, in force_text
raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128). You passed in <django.utils.functional.__proxy__ object at 0x40a6a90c> (<class 'django.utils.functional.__proxy__'>)
答案 0 :(得分:1)
好。问题在于翻译。在这种情况下 - 俄罗斯本地化。 不知道为什么从“原生”django本地化文件的翻译失败。 但对于所有有类似问题的人:
创建(如果不是)区域设置文件(https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#localization-how-to-create-language-files)
将theese行添加到var express = require('express'),
bodyParser = require('body-parser'),
oauthserver = require('oauth2-server');
var controllernew = require('./controllers/controllernew');
var app = express();
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.get('/index.html', controllernew.index);
app.oauth = oauthserver({
model: require('./models/modeloauth'),
grants: ['password'],
debug: true,
accessTokenLifetime: 31536000
});
app.get('/userOTP/:userid/username/:username', controllernew.getUserOTP);
app.all('/oauth/token', app.oauth.grant());
app.all('*', app.oauth.authorise());
app.use(app.oauth.errorHandler());
routes = require("./routes/items")();
app.use(routes);
var server = app.listen(3000, function(){
console.log('server started at ' + server.address().port);
});
:
index: function(req, res, next){
console.log("health check");
res.write("All ok");
res.end();
}
(或您需要的其他翻译)
编译(django.po
)