Search code examples
djangopython-3.xdjango-widget

Change the field class on error


I want to change the input field class and other attributes in case of error.

So in form init :

 for f_name in self.fields:
            if f_name in self.errors:
                self.fields[f_name].widget.attrs['error'] = a
            else:
                self.fields[f_name].widget.attrs['error'] = b

Now, I want to cycle thru the widget attributes and remove some attributes, not to be added to the html field(see error in this case).

{% for name, value in widget.attrs.items %}{% if name is not error %}{% if value is not False %} .....

the condition is not working:

{% if name is not error %}

I tried is not, != , is not in(error, alpha) also using error as string 'error'

are not working, but

{% if name == error %} is working

I don't understand why, because it should work as in normal python.

The value of error can variate.


Solution

  • It should be:

    {% if name != 'error' %}
    

    You are missing '', it's trying to compare it to another variable.