如何在 Django Cryspy 表单中添加表格?

问题描述

我想在 Django 脆皮表格中添加一个表格。 我正在尝试将以下 html 代码添加到我的 create_bill.html 或 forms.py 中,但我一直遇到一些语法错误添加以下 html 代码删除每一行的标题的正确方法是什么

Add lines table

<div class="card-block table-border-style">
     <div class="table-responsive">
         <table class="table table-hover">
             <thead>
                 <tr>
                    <th class="center">Item</th>                                                    
                    <th>Description</th>
                    <th class="center">Qty</th>
                    <th class="right">Unit Price</th>
                    <th>Account</th>
                    <th>Tax Rate</th>
                    <th class="right">Amount GBP</th>
                </tr>
             </thead>                                            
          </table>
       </div>
  </div>

forms.py

class BillForm(forms.ModelForm):

    bill_title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Title'}))
    vendor =  forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'vendor'}))
    bill_date =  forms.DateField(widget=forms.DateInput(attrs={'placeholder': 'Bill Date'}))
    due_date = forms.DateField(widget=forms.DateInput(attrs={'placeholder': 'Due Date'}))
    reference = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Reference'}))

    class Meta:
        model = Bill
        fields = ['vendor','bill_date','due_date','reference']  


    def __init__(self,*args,**kwargs):
        super(BillForm,self).__init__(*args,**kwargs)
        self.helper = FormHelper()
        self.helper.form_tag = True
        self.helper.layout = Layout(
            Row(
                Column('vendor',css_class='form-group col-md-2 mb-0'),Column('bill_title',Column('bill_date',Column('due_date',Column('reference',css_class='form-row'
            ),Fieldset('Add lines',Formset('lines')),Row(                               
                css_class='form-row'
            ),Div(               
                HTML("<br>"),ButtonHolder(Submit('submit','save')),)
            )

create_bill.html

{% extends 'base.html' %}

{% load crispy_forms_tags %}

{% block content %}
  {% crispy form %}
{% endblock %}

解决方法

你是否在 settings.py 中写了“CRISPY_TEMPLATE_PACK = 'bootstrap4'”?

,

我就是这样用的。你可以在html页面中尝试这样吗?

{% extends 'base.html' %}
    {% load static %}
    {% block body %}
    {% load crispy_forms_tags %}
    
    
    <form method="post">
        {% csrf_token %}
        {{form|crispy}}
    <br>
        <button type="submit" class="btn btn-danger">Kayıt Ol</button>
    </form>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...