Django有問題。Django GET方法。在分配之前引用的局部變量
我的錯誤信息:
UnboundLocalError在分配之前引用
回溯/城市/城 局部變量 'adv_city':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)
如何解決呢?
現在我有錯誤: render_to_string()最多需要3個參數(給出5個參數)http://dpaste.com/751737/ – pagepydj
@pagepydj這與前面的問題無關。查看['render_to_response']的文檔(https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response),可能你的意思是:'return render_to_response('advert_by_city ',{'adverts':廣告,'adv_city':adv_city},context_instance = RequestContext(request))''。 (如果你還想將'c'對象傳遞給模板,很好,但是將其作爲字典中的另一個字段,而不是作爲單獨的參數) – mgibsonbr