Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add plain text info to forms in a formset in Django?

I want to show a title and description from a db query in each form, but I don't want it to be in a charfield, I want it to be html-formatted text.

sample template code:

{% for form, data in zipped_data %}
   <div class="row">
      <div class="first_col">
         <span class="title">{{ data.0 }}</span>
         <div class="desc">
            {{ data.1|default:"None" }}
         </div>
      </div>
      {% for field in form %}
         <div class="fieldWrapper" style="float: left; ">
            {{ field.errors }}
            {{ field }}
         </div>
      {% endfor %}
{% endfor %}

Is this the most idiomatic way of doing this? Or, is there a way to add text that will not be displayed inside of a textarea or text input to my model:

class ReportForm(forms.Form):
   comment = forms.CharField()

?

like image 999
Josh Avatar asked Feb 20 '09 17:02

Josh


People also ask

Is a django form field that takes in a text input?

CharField() is a Django form field that takes in a text input. It has the default widget TextInput, the equivalent of rendering the HTML code <input type="text" ...> .

What is inline formset in django?

Django formset allows you to edit a collection of the same forms on the same page. It basically allows you to bulk edit a collection of objects at the same time.

What is form AS_P in django?

{{ form.as_p }} – Render Django Forms as paragraph. {{ form.as_ul }} – Render Django Forms as list.


2 Answers

Instead of zipping your forms with the additional data, you can override the constructor on your form and hold your title/description as instance-level member variables. This is a bit more object-oriented and learning how to do this will help you solve other problems down the road such as dynamic choice fields.

class MyForm (forms.Form):
    def __init__ (self, title, desc, *args, **kwargs):
        self.title = title
        self.desc = desc
        super (MyForm, self).__init__ (*args, **kwargs) # call base class

Then in your view code:

form = MyForm ('Title A', 'Description A')

Adjust accordingly if you need these values to come from the database. Then in your template, you access the instance variables just like you do anything else, e.g.:

   <h1>{{ form.title }}</h1>
   <p>{{ form.desc }}</p>

From the way you phrased your question, I think you probably have some confusion around the way Django uses Python class attributes to provide a declarative form API versus instance-level attributes that you apply to individual instances of a class, in this case your form objects.

  • Check out this link for a good discussion on the distinction
  • And this one
like image 149
Joe Holloway Avatar answered Sep 20 '22 13:09

Joe Holloway


I just created a read-only widget by subclassing the text input field one:

class ReadOnlyText(forms.TextInput):
  input_type = 'text'

  def render(self, name, value, attrs=None):
     if value is None: 
         value = ''
     return value

And:

class ReportForm(forms.Form):
  comment = forms.CharField(widget=ReadOnlyText, label='comment')
like image 36
Andrei Taranchenko Avatar answered Sep 23 '22 13:09

Andrei Taranchenko