Python 在django中重写ErrorList类时出现奇怪的UTF-8故障
当我试图通过继承ErrorList并重写其as_ul方法来适应引导错误html类时,我遇到了一个奇怪的问题 这是没有覆盖的表单:http://192.241.167.204/normal/ 这是带有覆盖的表单:http://192.241.167.204/override/ 请不要介意中文文本 两个表单都将始终验证为错误输入,未被我弄乱的表单将显示正确的警告消息: 但在我改变的状态下就没那么幸运了: 因为这是我在forms.py中所做的:Python 在django中重写ErrorList类时出现奇怪的UTF-8故障,python,django,linux,ubuntu,django-forms,Python,Django,Linux,Ubuntu,Django Forms,当我试图通过继承ErrorList并重写其as_ul方法来适应引导错误html类时,我遇到了一个奇怪的问题 这是没有覆盖的表单:http://192.241.167.204/normal/ 这是带有覆盖的表单:http://192.241.167.204/override/ 请不要介意中文文本 两个表单都将始终验证为错误输入,未被我弄乱的表单将显示正确的警告消息: 但在我改变的状态下就没那么幸运了: 因为这是我在forms.py中所做的: @python_2_unicode_compatibl
@python_2_unicode_compatible
class BootstrapErrorList(ErrorList):
def as_ul(self):
if not self: return ''
return format_html('<ul class="errorlist alert alert-error">{0}</ul>',
format_html_join('', '<li>{0}</li>',
((force_text(e),) for e in self)
)
)
def __str__(self):
return self.as_ul()
class BootstrapForm(forms.Form):
def __init__(self, *args, **kwargs):
new_kwargs = {'error_class': BootstrapErrorList}
new_kwargs.update(kwargs)
super(BootstrapForm, self).__init__(*args, **new_kwargs)
观点相当简单:
# -*- coding: UTF-8 -*-
# Create your views here.
from django.shortcuts import render
from test_app.forms import *
def without_override(request):
if request.method == 'GET':
form = FormWithoutOverride()
if request.method == 'POST':
form = FormWithoutOverride(request.POST)
if form.is_valid(): # will never be valid
pass
return render(request, 'normal_form.html', {'form': form})
def with_override(request):
if request.method == 'GET':
form = FormWithOverride()
if request.method == 'POST':
form = FormWithOverride(request.POST)
if form.is_valid(): # will never be valid
pass
return render(request, 'override_form.html', {'form': form})
模板的实现是非常简单的form.as\p,所以我将在这里跳过它
这个问题在我的开发平台Windows 7 Pro 64位上不存在,但它发生在我的部署平台Ubuntu 12.04 LTS 64位上,桌面和服务器都有这个问题。我不确定如果我换成其他linux平台或Mac会发生什么
这个问题与我是否将它放在Apache2+mod_wsgi或manage.py runserver中无关,gunicorn也不重要。它们都有相同的问题,因此可能不是特定于部署的问题
我完全不知道我做错了什么?任何潜在客户都将不胜感激。您正在使用Python 2吗?如果是,则不要写:
format_html('<ul class="errorlist alert alert-error">{0}</ul>',
format_html_join('', '<li>{0}</li>',((force_text(e),) for e in self)))
写:
format_html(u'<ul class="errorlist alert alert-error">{0}</ul>',
format_html_join(u'', u'<li>{0}</li>', ((force_text(e),) for e in self)))
在前面添加u可以让python知道您想要的是unicode字符串而不是ASCII字符串。您使用的是python 2吗?如果是,则不要写:
format_html('<ul class="errorlist alert alert-error">{0}</ul>',
format_html_join('', '<li>{0}</li>',((force_text(e),) for e in self)))
写:
format_html(u'<ul class="errorlist alert alert-error">{0}</ul>',
format_html_join(u'', u'<li>{0}</li>', ((force_text(e),) for e in self)))
在前面添加u可以让python知道您想要的是unicode字符串而不是ASCII字符串。哇,真见鬼!这解决了问题,但我仍然不知道这怎么可能?由于原始错误列表不使用u'unicode\u string',但它们仍然传递如何传递,为什么传递?包含原始错误列表的文件的第一行是来自uuu future\uuuu导入unicode\u文本。这告诉Python2对该文件使用Python3Unicode语义,这意味着不需要u。。。我自己花了一段时间才弄明白;如果我们想扩展所有内容,那么我们应该从原始内容导入所有内容?这是一个教训…艰难的道路非常感谢你,梅西。非常感谢你的帮助!谢谢!我没意识到哇,真见鬼!这解决了问题,但我仍然不知道这怎么可能?由于原始错误列表不使用u'unicode\u string',但它们仍然传递如何传递,为什么传递?包含原始错误列表的文件的第一行是来自uuu future\uuuu导入unicode\u文本。这告诉Python2对该文件使用Python3Unicode语义,这意味着不需要u。。。我自己花了一段时间才弄明白;如果我们想扩展所有内容,那么我们应该从原始内容导入所有内容?这是一个教训…艰难的道路非常感谢你,梅西。非常感谢你的帮助!谢谢!我没有意识到这一点