Getting this error: Exception Value:
Could not parse the remainder: ' + contact.last_name' from 'contact.first_name + contact.last_name'I am having trouble displaying a list of names, with each name as a link.
My models.py code:
class Contact(models.Model):
first_name = models.CharF开发者_StackOverflowield("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)
My views.py code:
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})
My index.html template:
{% 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 %}
You can just do:
{{ contact.first_name }} {{ contact.last_name }}
Django doesn't know what to do with the +
, and you don't need it.
for a not so long contact list , eg:
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})
精彩评论