1👍
First of all, I suggest using a TextField
instead of CharField
. General CharField
is intended for a small snippet of text and TextField
is for a large text.
If you insist on using a CharField
, you should format this long line of code in such a way that you can understand what it does. For example:
general_message = forms.CharField(
label='General message*',
required=True,
widget=forms.Textarea,
validators=[
RegexValidator(
regex=r'http(s)?|HTTP(s)?',
message="No http or https allowed",
code="invalid"
)
]
(
attrs={
'class': 'form-control',
'maxlength': '1000',
'rows': 8
}
)
)
Now you see that you have a ()
immediately after the []
for validators
. This looks like a function call. But that doesn’t make any sense since you cannot call a list, as the error says.
By using consistent formatting and indentation, you can easily avoid these kinds of problems. The format I show above is one of many styles that can work. Just pick something and stick with it.
To fix this, you need to just move the code around so it lines up correctly. It looks like you put the validators
in the wrong place in between Textarea
and the ()
to call its constructor. The correct code should be like this:
general_message = forms.CharField(
label='General message*',
required=True,
widget=forms.Textarea(
attrs={
'class': 'form-control',
'maxlength': '1000',
'rows': 8
}
),
validators=[
RegexValidator(
regex=r'http(s)?|HTTP(s)?',
message="No http or https allowed",
code="invalid"
)
]
)