python - 条目未显示在 django 表中
问题描述
问题:我有一个联系人模型。在这个联系模型中,我有多个类型(供应商、客户和员工)。数据已上传到 SQL 表中,但未显示在 Django/html 表中。我是否需要更改模型的编写方式?
提前致谢
模型:
class Contact(models.Model):
CONTACT_TYPE = (
(0, 'Customer'),
(1, 'Supplier'),
(2, 'Employee'),
)
GENDER = (
(0, 'Male'),
(1, 'Female'),
)
type = models.IntegerField(choices=CONTACT_TYPE,default=1)
gender = models.IntegerField(choices=GENDER,default=0)
company = models.ForeignKey(Company, on_delete=models.CASCADE, blank=True, null=True)
意见:
def index_supplier(request):
contacts = Contact.objects.all()
suppliers = Contact.objects.filter(type=1).all()
count_supplier= Contact.objects.filter(type=1).count()
context = {
'contacts': contacts,
'count_supplier': count_supplier,
}
return render(request, 'contacts/index_supplier.html', context)
模板:
<div class="container-fluid">
<div class="col-sm-20">
<table id="dtBasicExample" class="table table-striped table-hover">
<script src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function(){
$('#dtBasicExample').DataTable();
});
</script>
<thead>
<tr>
<th>Type</th>
<th>Contact Name</th>
<th>Company</th>
</tr>
</thead>
<tbody>
{% for contact in suppliers.all %}
<tr>
<td>{{ contact.get_type_display }}</td>
<td>{{ contact.first_name }}</td>
<td>{{ contact.company }}</td>
</tr>
{% endfor %}
</div>
</div>
解决方案
您需要修复context
变量:
context = {
'contacts': contacts,
'count_supplier': count_supplier,
'suppliers': suppliers,
}
推荐阅读
- javascript - flv.js 无法使用 avc1 编解码器
- html - 将值从一个表传递到另一个 html 页面
- c++ - 如何使用中值滤波算法去除胡椒噪声
- c++ - 在 if 语句中声明时如何访问类对象?(在 C++ 中)
- java - java - 如何在java swing中加载本地html文件?
- centos7 - Clearcase - 无法访问 VOB - ls:读取目录 - 权限被拒绝
- android - 如何在 viewpager 中使用 webview 的水平滚动?
- swift - 文本文件中的 Swift 转换格式(字符串解析)
- spring-boot - spring boot配置自定义sql
- c# - 如何获取列表框中所选项目的索引值?