2015-08-14 98 views
2

這看起來好像很簡單,但我一直無法找到解決這個特定問題的答案。 本質上,我想讓我的Django模板循環瀏覽在該頁面視圖中創建的列表。但是,當我嘗試運行它時,我得到「屬性錯誤: '列表'對象沒有屬性'get'」。我一直試圖擴展Django民意調查應用程序,這個應用程序的想法是讓書籍按作者排序進行投票。因此,這個觀點將會顯示一個表格,其中一邊是作者的名字,另一邊是每個作者書中的總票數。
這裏是模型。
django模板中的循環 - 列表對象沒有屬性'get'

class Author(models.Model): 
    author_name = models.CharField(max_length=200) 

    def __unicode__(self): 
     return self.author_name 


class Book(models.Model): 
    author = models.ForeignKey(Author) 
    title = models.CharField(max_length=200) 
    votes = models.IntegerField(default=0) 

    def __unicode__(self): 
     return self.title 

這裏是我的作家試圖組的票,並將它們添加到列表視圖。

def totals(request): 
    a = Author.objects.order_by('author_name') 
    b = Book.objects.order_by('author__author_name') 
    total = [] 
    for i in range(len(b)): 
     if i < len(b)-1: 
      x = b[i].votes 
      if b[i].author == b[i+1].author: 
       x += b[i+1].votes 
      else: 
       total.append(x) 
     else: 
      x = b[i].votes 
      total.append(x) 
      return total 

    return render(request, "book/totals.html", {"a":a, "total":total}) 

這裏是模板。第一個for循環的「a」工作正常,第二個應該循環通過「總計」,這是行不通的。

<h1>Total Votes</h1> 
<table style="border-collapse:collapse;"> 
<thead> 
    <tr> 
     <th colspan="2"><strong>Totals</strong></th> 
    </tr> 
    <tr style="border-bottom:1px solid black;"> 
     <th style="padding:5px;"><em>Authors</em></th> 
     <th style="padding:5px;border-left:1px solid black;"><em>Votes</em></th> 
    </tr> 
    </thead> 

    {% for author in a %} 
     <tr> 
      <td>{{ author }}</td> 
     </tr> 
    {% endfor %} 
    {% for x in total %} 
     <tr> 
      <td>{{ total[x] }}</td> 
     </tr> 
    {% endfor %} 
</thead> 

好吧,我想,大約做的。感謝任何讀到此爲止的人。很顯然,我對此很新,所以如果有任何其他評論或任何人的反饋,我一定會很感激聽到他們。 謝謝!

編輯:這裏是回溯 -

Environment: 


Request Method: GET 
Request URL: http://127.0.0.1:8000/book/totals/ 

Django Version: 1.8.3 
Python Version: 2.7.10 
Installed Applications: 
('django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'book') 
Installed Middleware: 
('django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware', 
'django.middleware.security.SecurityMiddleware') 


Traceback: 
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    223.     response = middleware_method(request, response) 
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/middleware/clickjacking.py" in process_response 
    31.   if response.get('X-Frame-Options', None) is not None: 

Exception Type: AttributeError at /book/totals/ 
Exception Value: 'list' object has no attribute 'get' 

回答

1

你的看法應該返回一個HTTP響應。但是,您的觀點返回total,這是一個list

  return total 

您應該刪除這一行,或者改變它,所以它會返回一個HTTP響應。

+0

這樣做!謝謝! – seeonsee

1

請儘量只用X,因爲X是一個對象不是一個索引。

{% for x in total %} 
     <tr> 
      <td>{{ x }}</td> 
     </tr> 
    {% endfor %} 

我希望是有用的。

+0

感謝您的回覆。我嘗試使用「x」而不是「total [x]」,它仍然拋出「'list'對象沒有屬性'get'」錯誤。 – seeonsee

+0

@seeonsee分享你的完整追蹤 – Gocht

相關問題