推荐学习书目
› 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 · Oct 16, 2013 · 3351 views
    This topic created in 4729 days ago, the information mentioned may be changed or developed.
    用了def clean_somefield()方法;

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

    也尝试修改了django\conf\locale\zh_CN\LC_MESSAGES\django.po下的本地化内容都没用。
    2 replies  •  1970-01-01 08:00:00 +08:00
    jokaye
        1
    jokaye  
       Oct 16, 2013
    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
       Oct 17, 2013
    @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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1169 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 23:58 · PVG 07:58 · LAX 16:58 · JFK 19:58
    ♥ Do have faith in what you're doing.