经过多次尝试和错误后,我无法将模型显示到模板中,我尝试使用TemplateTags或使用视图,也许我错过了一些步骤......
我通过夹层名称“客户数据”(url as customer-data)创建了一个页面,然后创建了一个模板名称“customer-data.html”
models.py
class CustomerData(models.Model):
number = models.CharField(max_length=50, help_text="Contact")
first_name = models.CharField(max_length=50, help_text="First Name")
last_name = models.CharField(max_length=50, help_text="Last Name")
email = models.CharField(max_length=200, help_text="Email")
address = models.TextField(max_length=200, help_text="Address")
owner = models.ForeignKey(user_model_name, verbose_name="Author")
class Meta:
verbose_name = _("Customer")
verbose_name_plural = _("Customers")
def __unicode__(self):
return "%s %s (%s)" % (self.first_name, self.last_name, self.number)
views.py
def customer_data(request):
customer_data_list = CustomerData.objects.all()
return render(request, 'customer-data.html', {'customer_data_list': customer_data_list})
cust_tags.py
@register.as_tag
def customer_data(*args):
data = CustomerData.objects.all()
return list(data)
客户data.html
{% block main %}
{{ page.description }}
<div class='contentWrapper'>
{% for custdata in customer_data_list %}
{{ custdata.first_name }}
<br/>
{% endfor %}
</div>
{% endblock %}
任何使用模板标签或视图的好例子都会有很大的帮助,谢谢你。