gpt4 book ai didi

python - Django self.cleaned_data 问题。

转载 作者:行者123 更新时间:2023-11-30 22:56:54 29 4
gpt4 key购买 nike

在 Django 中创建了一个完全工作的表单 - 但是当我尝试定义一个函数来引发自定义 form.ValidationError 时,它会中断。

 # importing form modules

from django import forms
import re

# define the forms

class single_input(forms.Form):

CHROMOSOMES = (

('1' , '1'),
('2' , '2'),
('3' , '3'),
('4' , '4'),
('5' , '5'),
('6' , '6'),
('7' , '7'),
('8' , '8'),
('9' , '9'),
('10' , '10'),
('11' , '11'),
('12' , '12'),
('13' , '13'),
('14' , '14'),
('15' , '15'),
('16' , '16'),
('17' , '17'),
('18' , '18'),
('19' , '19'),
('20' , '20'),
('21' , '21'),
('22' , '22'),
('23' , '23'),
('X' , 'X'),
('Y ' , 'Y')

)

def clean_testname(self):

print self.cleaned_data

testname = self.cleaned_data['testname']
genome_pos = self.cleaned_data['genome_pos']


if ( not re.match(r"^\w+$", testname)):

raise forms.ValidationError(

"Test name is only allowed letter's number's and _'s ."
"NO spaces or funny things that involve the shift button"
)

if ( not re.match(r"^\s+$", genome_pos)):

raise forms.ValidationError(

"Genome position is only allowed numbers and -'s"
"NO spaces, letter or funny things that involve the shift button"

)


return cleaned_data


testname = forms.CharField ( label='testname', max_length=100 )
chromosome = forms.ChoiceField( label='chromosome', choices = CHROMOSOMES , required = True)
genome_pos = forms.CharField ( label='genome_pos', max_length=15 )

问题是它只将第一个表单字段放入cleaned_data中,因此上面代码中的print clean_data看起来像:

{'testname': u'ssss'}

如果我注释掉整个 clean_testname 函数,我会得到一个工作输出

{'genome_pos': u'xxx', 'testname': "name", 'chromosome': u'1'}

最佳答案

问题是您正在尝试清理 clean_testname 方法中的 testnamegenome_pos 字段。

您应该清理 clean_testname 方法中的 testname 字段,以及 clean_genome_pos 方法中的 genome_pos 字段.

如果你想validate fields that depend on each other ,那么这属于 clean 方法。在本例中,您似乎不需要 clean 方法,因为字段名称似乎并不相互依赖。

class SingleInput(forms.Form):
# It would be better to name the form SingleInput rather than single_input

def clean_testname(self):
test_name = self.cleaned_data['testname']
# validate testname, raise ValidationError if necessary.
# you can't access any other fields from self.cleaned_data here
return testname

def clean_genome_pos(self):
test_name = self.cleaned_data['genome_pos']
# validate genome_pos, raise ValidationError if necessary.
# you can't access any other fields from self.cleaned_data here
return genom_post

def clean(self):
cleaned_data = super(SingleInput, self).clean()

# you need to handle case when the fields do not exist in cleaned_data
testname = cleaned_data.get('testname')
genome_pos = cleaned_data.get('genome_pos')

if testname and genome_pos:
# do any checks that rely on testname *and* genome_pos, and
# raise validation errors if necessary
...

return cleaned_data

您的情况的另一个选择是使用 RegexField 而不是自定义清理方法。

class SingleInput(forms.Form):
testname = forms.RegexField(
label='testname',
max_length=100,
regex=r"^\w+$",
message="<error message>",
)
...

关于python - Django self.cleaned_data 问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36895306/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com