简体   繁体   English

如何在Django的formset中向表单添加纯文本信息?

[英]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. 我想在每个表单中显示db查询的标题和描述,但我不希望它在charfield中,我希望它是html格式的文本。

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: 或者,有没有办法添加不会在textarea或文本输入中显示的文本到我的模型:

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

?

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, eg: 然后在您的模板中,您可以像访问其他任何内容一样访问实例变量,例如:

   <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. 从您提出问题的方式来看,我认为您可能会对Django使用Python 类属性提供声明性表单API与应用于类的各个实例的实例级属性的方式有一些混淆,在这种情况下,您的表单对象。

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')

I had to solve a similar problem and like your idea Andrei. 我不得不解决类似的问题,就像你的想法安德烈。 I had some issues using it though, as, if there were validation errors, the value of the read-only field would get lost. 我有一些使用它的问题,因为,如果有验证错误,只读字段的值将丢失。 To solve this, I did something similar but overrode HiddenInput instead and kept the value in a hidden form field. 为了解决这个问题,我做了类似的事情但改写了HiddenInput,并将值保存在隐藏的表单字段中。 ie: 即:

class ReadOnlyText(forms.HiddenInput):
    input_type = 'hidden'

    def render(self, name, value, attrs=None):
        if value is None:
            value = '' 
        return mark_safe(value + super(ReadOnlyTextWidget, self).render(name, value, attrs))

class ReportForm(forms.Form):
  comment = forms.CharField(widget=ReadOnlyText, label='comment')

I think you can get it with "{{ field.value }}". 我想你可以用“{{field.value}}”来获得它。 Maybe it's the easier way. 也许这是更容易的方式。

{% for form in formset %}
    {% for field in form %}
        {% if forloop.counter = 1 %}
            <td><img src="{{ MEDIA_URL }}{{ field.value }}"/></td>
        {% endif %}
        {% if forloop.counter = 2 %}
            <td>{{ field.value }}</td>
        {% endif %}
        {% if forloop.counter > 2 %}
            <td>{{ field }}{{ field.errors }}</td>
        {% endif %} 
    {% endfor %}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM