问题描述
我想在 Django 脆皮表格中添加一个表格。 我正在尝试将以下 html 代码添加到我的 create_bill.html 或 forms.py 中,但我一直遇到一些语法错误。添加以下 html 代码并删除每一行的标题的正确方法是什么:
<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>