請幫助我在django模板中顯示字典的值。我試圖找出谷歌,但無法得到解決方案。訪問django模板中的字典值
下面是Model
class Ride(models.Model):
type = models.BooleanField(default=False)
add_source = models.ForeignKey(Address, related_name='source')
add_destination = models.ForeignKey(Address, related_name='destination')
ride_comment = models.TextField(null=True,max_length=140,blank=True)
def __unicode__(self):
return self.ride_comment
class Driver(models.Model):
ride_id = models.ForeignKey(Ride)
user_id = models.ForeignKey(User)
drv_carseats = models.SmallIntegerField(null=True,blank=False)
def __unicode__(self):
return self.user_id.username
查看
for ride in result_list:
if ride.type:
driver = Driver.objects.get(ride_id = ride)
userList[ride.pk] = driver.user_id.username
print 'driver', driver.user_id.username, userList[ride.pk]
return render_to_response('rides/search.html', {'result_list':result_list,'userList':userList}, context)
這裏是我的模板代碼
{% for result in result_list %}
{% if result %}
<a href="/rides/ridedetails/{{ result.pk }}">{{ userList[result.pk] }}</a>
<em>{{ result.add_source }}</em>
<em>{{ result.add_destination }}</em>
<em>{{ result.ride_comment }}</em>
{% endif %}
{% endfor %}
我收到以下錯誤
TemplateSyntaxError在/遊戲機/搜索/
無法解析餘數: '[result.pk]' 從 '用戶列表[result.pk]'