Django:显示管理员验证错误的自定义错误消息 [英] Django: Display a custom error message for admin validation error

查看:151
本文介绍了Django:显示管理员验证错误的自定义错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.2.4。我有一个需要验证的字段的模型。验证失败时,我想向用户显示自定义错误消息。模型编辑在管理界面中完成。



这是我目前正在做的:

  def clean_fields (自我,排除=无):
#验证
如果问题:
raise ValidationError({'field_name':错误消息})

不幸的是,所有这些都是在管理页面上打印出一个单独的验证消息,每个字符的值为 field_name



什么是正确的方式来显示我想要的错误消息?

解决方案

不看,听起来管理员正在寻找一个可迭代的值作为 field_name 的值。尝试:

  raise ValidationError({'field_name':[error message,]})

我认为管理员希望任何数量的验证消息与表单上的每个字段相关联。


I'm using Django 1.2.4. I have a model that has a field that needs to be validated. When validation fails, I'd like to display a custom error message to the user. Model editing is done in the admin interface.

This is what I'm doing currently:

def clean_fields(self, exclude=None):
    # do validation
    if problem:
        raise ValidationError({'field_name': "error message"})

Unfortunately, all this does is print out a separate validation message on the admin page for each character in the value of field_name.

What is the proper way to signal the error message I want?

解决方案

Without looking, it sounds like the admin is looking for an iterable as the value for field_name. Try:

raise ValidationError({'field_name': ["error message",]})

I think the admin expects any number of validation messages to be associated with each field on a form.

这篇关于Django:显示管理员验证错误的自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆