3

I have a form class that looks like..

#forms.py

class ExampleForm(forms.Form):
    color = forms.CharField(max_length=25)
    description = forms.CharField(widget=forms.Textarea, max_lenght=2500)

and my view looks like this..

#views.py



class EditExample(UpdateView):
model = Example
fields = ['color', 'description']
template_name = 'example.html'

def get_success_url(self):
    pass

Template:

#example.html
{% for field in form %}
    {% if field.errors %}
            <div class="control-group error">
                <label class="control-label">{{ field.label }}</label>
                <div class="controls">{{ field }}
                    <span class="help-inline">
                        {% for error in  field.errors %}{{ error }}{% endfor %}
                    </span>
                </div>
            </div>
            {% else %}
                <div class="control-group">
                <label class="control-label">{{ field.label }}</label>
                <div class="controls">{{ field }}
                    {% if field.help_text %}
                        <p class="help-inline"><small>{{ field.help_text }}</small></p>
                    {% endif %}
                </div>
     {% endif %}
{% endfor %}

When the template is rendered, all the fields show up and populate correctly but the 'description' doesn't show up in a Textarea but rather in a normal field. Im assuming this is because UpdateView works off of 'model = something' rather than 'form = something'.

I have tried..

#views.py
class EditExample(UpdateView):
    model = Example
    form_class = Exampleform
    template_name = 'example.html'

    def get_success_url(self):
        pass

but I get a Django error saying "init() got an unexpected keyword argument 'instance'.

Is there any way to successfully get the Description to render in a Textarea using Updateview? If not, how would I accomplish this?

Thanks!

L.hawes
  • 189
  • 3
  • 14

1 Answers1

5

Your form needs to inherit from forms.ModelForm, not the base Form class.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I created a ModelForm and plugged it into the view with 'form_class ='. Everything works great but Im still not getting the description to show up in a text area widget. Its a pain in the butt to try to edit a description thats hundreds of words long in a normal sized form! Any more ideas? Thank you – L.hawes Aug 26 '16 at 13:36
  • How do you change an object when form got submitted, in ***Views.py***? – AnonymousUser May 31 '22 at 04:42