0

I'm attempting to add an attribute to a django crispy form using Django-Widget-Tweaks. I followed instructions, however, instead of adding the 'disabled' attribute to the existing field, it's adding a whole new field to the form with the same name, id, class, etc and includes my'disabled' attribute (lol). Here's my widget tweak code...

        {% crispy registration_form %}
        {{ registration_form.net_profit|attr:"disabled" }}

This is an image to the inspected html code that is generated. Sorry, I couldn't figure out how to copy the code.:

enter image description here

Any help is appreciated!

Red
  • 26,798
  • 7
  • 36
  • 58
  • 1
    You can copy the code, paste it into your post, and add three ticks to the top and bottom of the code – Red Jul 26 '20 at 02:22
  • Hi, Thanks but my problem isn't pasting the code here, as I have done so above, it was copying the html code from the inspect window. It wouldn't allow me to select the lines to copy. –  Jul 26 '20 at 03:22
  • Why don't you disable if from your form declaration before sending to the view – Ceetified_karma Jul 26 '20 at 04:33
  • Hi, I wish it was that simple. As I'm using crispy forms, the field declarations will not accept 'disabled' as an attribute. I receive a syntax error. Here is a snippet from my declaration... ``` class RegistrationForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(RegistrationForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.layout = Layout( Div( Div('organization_name',css_class='form-group col-md-3'), Div('phone', css_class='form-group col-md-3'), ``` –  Jul 26 '20 at 13:19
  • With help, I did get the disable to work with the following statement... ``` $("input#id_ticket_count").attr("disabled", "disabled"); ``` However, with those fields disabled, even though I was plugging values into them through code, the form wouldn't save as Django still treated them as empty. Back to the drawing board. –  Aug 02 '20 at 03:27

0 Answers0