我对决哥有意见
我的错误消息 :
UnboundLocalError at /city/city local variable adv_city referenced before assignment
观点:
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)
如何修补?