V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
halicando
V2EX  ›  Python

form无法自定错误信息

  •  
  •   halicando · 2013-10-16 11:18:03 +08:00 · 2509 次点击
    这是一个创建于 3816 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用了def clean_somefield()方法;

    也用了这个方法 http://www.oschina.net/question/247206_45024 ;

    也尝试修改了django\conf\locale\zh_CN\LC_MESSAGES\django.po下的本地化内容都没用。
    2 条回复    1970-01-01 08:00:00 +08:00
    jokaye
        1
    jokaye  
       2013-10-16 14:32:27 +08:00
    class YourForm(ModelForm):

    def __init__(self, *args, **kwargs):
    super(YourForm, self).__init__(*args, **kwargs)
    self.error_message = 'whatever you want'

    def clean_somefield(slef):
    if not check_somefield_valid(): //do your check in here
    raise formValidateError(self.error_message)
    return self
    halicando
        2
    halicando  
    OP
       2013-10-17 20:16:31 +08:00
    @jokaye

    好像还是不行

    版本信息:django1.3.1 ,python2.7.1

    以下是代码:

    class TopicForm(forms.Form):

    title=forms.CharField(max_length=50,label='标题')
    content=forms.CharField(max_length=300,widget=forms.Textarea(),label='内容')

    def __init__(self, *args, **kwargs):
    super(TopicForm, self).__init__(*args, **kwargs)
    self.error_message = u'不超过300字'

    def clean_title(self):
    title = self.cleaned_data['title']
    if len(title) > 50:
    raise forms.ValidationError(u'不超过50字')
    return title

    def clean_content(self):
    content = self.cleaned_data['content']
    if len(content) > 300:
    raise forms.ValidationError(self.error_message)
    return content
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1244 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:00 · PVG 02:00 · LAX 11:00 · JFK 14:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.