首页 > 解决方案 > 我不想在 html 中显示表单的标签

问题描述

我不想在 html 中显示表单的标签。我写在forms.py

class SearchForm(forms.Form):
   keyword = forms.CharField(min_length=2, max_length=100)

在views.py中

def index(request):
    form = SearchForm()
    return render(request, 'index.html',{'form':form})

在 index.html 中

<div>
      <form action="{% url 'app:search' %}" method="POST">
         <table>
         {{ form|as_bootstrap }}
         </table>
         <input type="image" src="{% static 'app/box.png' %}">
        {% csrf_token %}
      </form>
</div>

当我运行应用程序时,会显示表单标签的关键字。我不想展示它。我的代码有什么问题?我读了as_p删除表单的标签,所以我重写了代码,

{{ form.as_p|as_bootstrap }}

但它仍然是一样的。我应该如何解决这个问题?

标签: pythondjango

解决方案


as_p不会删除标签,它只会在标签内呈现标签/输入对<p>。要设置空标签,您可以使用字段的label参数:

class SearchForm(forms.Form):
   keyword = forms.CharField(min_length=2, max_length=100, label='')

推荐阅读