[英]Django Template Attributes of QuerySet for ManyToMany with “Through” Class
[英]How do I pass multiple class attributes through django to the template?
收到此错误:异常值:
无法解析“ contact.first_name + contact.last_name”中的其余部分:“ + contact.last_name”
我在显示名称列表时遇到麻烦,每个名称都作为链接。
我的models.py代码:
class Contact(models.Model):
first_name = models.CharField("First Name", max_length=30)
last_name = models.CharField("Last Name", max_length=30)
def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
我的views.py代码:
from django.http import HttpResponse
from pk.models import Contact
from django.template import Context, loader
from django.shortcuts import render_to_response
def index(request):
contact_list = Contact.objects.all()
return render_to_response('pk/index.html', {'contact_list': contact_list})
我的index.html模板:
{% if contact_list %}
<ul>
{% for contact in contact_list %}
<li><a href="/pkl/{{ contact.id }}/">{{ contact.first_name + contact.last_name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No contacts are available.</p>
{% endif %}
您可以这样做:
{{ contact.first_name }} {{ contact.last_name }}
Django不知道如何使用+
,并且您不需要它。
对于不太长的联系人列表,例如:
def index(request):
contact_list = Contact.objects.all()
for i in contact_list:
contactlist.append(i.first_name + i.last_name)
return render_to_response('pk/index.html', {'contact_list': contactlist})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.