我对 Django 有疑问。
我的错误信息:
分配前引用的 /city/city 局部变量 'adv_city' 处的 UnboundLocalError
追溯:http : //dpaste.com/751727/
意见:
def advert_by_city(request):
c = request.GET.get('city', '')
if c:
adv_city = Advert.objects.filter(city=c)
adverts = Advert.objects.values('city').distinct()
return render_to_response('advert_by_city', {'adverts':adverts},{'adv_city':adv_city}, context_instance=RequestContext(request))
模板:
{% for city in adverts %}
<a href="city?{{ city.city }}"/>{{city.city}}</a>
<br/>
{% endfor %}
{% for adverts in adv_city%}
{{adverts.title}}
{% endfor%}
模型:
class Advert(models.Model):
title = models.CharField(max_length=255)
city = models.CharField(max_length=255)
如何解决?