0

Ok. I have completely changed the question, though, thank you for the replies of how to create a text area in a django form.

My problem definition is this: I need to display a page which has 5 randomly selected questions from a model. Interjected in between each model would be a textarea for the person to write the answer and submit it. All the answers will be stored in a way that they can be referenced in the future on the basis of the questions.

I can create a form-set of text-boxes but how do I display the questions? Modelforms?

Hick
  • 35,524
  • 46
  • 151
  • 243

2 Answers2

0
  1. You often have to restart your Django app for code changes to take effect

  2. There are several ways to display Textarea widget for CharField, without which sounds like what you want .

From the Django docs:

# override individual field
class AuthorForm(ModelForm):
    class Meta:
        model = Author
        fields = ('name', 'title', 'birth_date')
        widgets = {
            'name': Textarea(attrs={'cols': 80, 'rows': 20}),
        }

# in admin site, override all fields of a certain type
class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.CharField: {'widget': forms.Textarea},
    }

You could also make a custom field, sub-classing CharField and specify Textarea as the default widget then use that in your models.

Probably the best way though (for ModelForm only) is the one in this answer:
How do you change the default widget for all Django date fields in a ModelForm?

writing a formfield_callback function and attaching it to your ModelForm class.

Community
  • 1
  • 1
Anentropic
  • 32,188
  • 12
  • 99
  • 147
0

Ok, to answer your new question...

You're going to have an Answer model, with a foreign key back to a Question

So your answer textareas could be an inline formset against each Question: https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#inline-formsets

Anentropic
  • 32,188
  • 12
  • 99
  • 147