L.hawes
L.hawes

Reputation: 199

Django UpdateView forms

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!

Upvotes: 3

Views: 7444

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599956

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

Upvotes: 6

Related Questions