2013-09-30 175 views
0

我收到以下錯誤消息。我無法理解爲什麼會發生以及如何解決它。NameError:未定義全局名稱'parentid_id'

Internal Server Error: /location/0/ 
Traceback (most recent call last): 
    File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, i 
n get_response 
    response = callback(request, *callback_args, **callback_kwargs) 
    File "D:\Project\myproject\radio\views.py", line 33, in location_show 
    arrs = Regions.objects.filter(parentid_id in ListFirst) 

Modles.py

class Regions(models.Model): 
    id =models.AutoField(primary_key=True) 
    name = models.CharField(max_length=30) 
    english_name = models.CharField(max_length=20) 
    parentid = models.ForeignKey('self', blank=True,null=True) 
    slug_name = models.SlugField(max_length=40,blank=True) 
    regions_level = models.PositiveIntegerField() 
    comment = models.CharField(max_length=50,blank=True,null=True) 

class Radio(models.Model): 
    station_name = models.CharField(max_length=30) 
    description = models.TextField() 
    regions = models.ForeignKey(Regions,blank=True,null=True) 
    custom_url = models.SlugField(max_length=100,blank=True) 
    stream = models.ForeignKey(Stream,related_name='radio_stream') 
    attributes = models.OneToOneField(Attributes,blank=True,null=True) 
    type = models.ForeignKey(Type,related_name='radio_type') 
    tags = models.ManyToManyField(Tag, blank=True) 
    comment = models.ForeignKey(Comment,related_name='radio_comment',blank=True,null=True) 
    publish_time = models.DateTimeField(auto_now_add=True) 
    update_time = models.DateTimeField(auto_now_add=True) 

Views.py

def location_show(request, id): 
    try: 
     radio_single = Regions.objects.get(id = id) 
     if radio_single.regions_level == 0: 
      ars = Regions.objects.filter(parentid_id=id) 
      ListFirst = [f.id for f in ars] 
      arrs = Regions.objects.filter(parentid_id in ListFirst) 
      ListSencond = [s.id for s in arrs] 
      arrss = Regions.objects.filter(parentid in ListSecond) 
      ListThird = [t.id for t in arrss] 
      listFirst.extend(ListSecond) 
      ListFirst.extend(listThird) 
      ListFirst.append(id) 
      radios = Radio.objects.filter(regions in ListFirst) 
      return render_to_response("radio_location_show.html",{"ars":ars,"radios":radios}) 
     elif radio_single.regions_level == 1: 
      ars = Regions.objects.filter(parentid_id=id) 
      listFirst = [p.id for p in ars] 
      arss = Regions.objects.filter(parentid in ListFirst) 
      ListsSecond = [s.id for s in arss] 
      ListFirst.extend(ListSecond) 
      ListFirst.append(id) 
      radios = Radio.objects.filter(regions in ListFirst) 
      return render_to_response("radio_location_show.html",{"ars":ars,"radios":radios}) 
     elif radio_single.regions_level == 2: 
      ars = Regions.objects.filter(parentid_id=id) 
      ListFirst= [p.id for p in ars] 
      ListFirst.append(id) 
      radios = Radio.objects.filter(regions in ListFirst)  
      return render_to_response("radio_location_show.html",{"ars":ars,"radios":radios}) 
     else: 
      ars = Regions.objects.filter(parentid_id = id) 
      ListFirst = [f.id for f in ars] 
      radios = Radio.objects.filter(regions in ListFirst) 
      return render_to_response("radio_location_show.html",{"ars":ars,"radios":radios}) 
     except Radio.DoesNotExist: 
      raise Http404 

會很感激的幫助不大,如果有人知道我要去的地方錯了,可以解釋。

+0

你有一個錯字(實際上2) - 'listFirst'應該是'ListFirst' – karthikr

回答

2

這不是Django的語法(它多次使用在你的代碼):

arrs = Regions.objects.filter(parentid_id in ListFirst) 

你大概的意思

arrs = Regions.objects.filter(parentid__id__in=ListFirst) 

你也可能要改線:

ars = Regions.objects.filter(parentid_id=id) 

有雙下劃線:

ars = Regions.objects.filter(parentid__id=id) 
+0

Gah,我有這個相同的答案准備發佈時,OP刪除downvoted問題並打開一個新的代替.. –

+0

@MartijnPieters :Yeesh。這是有點粗魯的OP。 –

+0

@ MartijnPieters,非常抱歉,我是一個新的Python精簡版。我將來會關注我的錯誤。感謝您的通知。 – user2831966

相關問題