2014-01-29 45 views
2

我想創建一個存檔,所以我傳遞給視圖參數年和月。Django得到了一個意想不到的關鍵字參數

但是,我得到一個錯誤與下面的代碼,我想不出這意味着什麼,如何解決這個問題:

Exception Type: TypeError 
Exception Value: archive() got an unexpected keyword argument 'year_id' 
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response, line 115 

什麼可能是錯誤的?

Views.py

def mkmonth_lst(): 
if not Post.objects.count(): 
    return [] 

# set up vars 
year, month = time.localtime()[:2] 
first = Post.objects.order_by("created")[0] 
fyear = first.created.year 
fmonth = first.created.month 
months = [] 

# loop over years and months 
for y in range(year, fyear-1, -1): 
    start, end = 12, 0 
    if y == year: start = month 
    if y == fyear: end = fmonth-1 

    for m in range(start, end, -1): 
     months.append((y, m, month_name[m])) 

return months 

def archive(request, year, month): 
posts = Post.objects.filter(created__year=year, created__month=month) 
context = {'PostList': posts, 'Months': mkmonth_lst()} 

return(render, 'archives.html', context) 

urls.py

url(r'^archives/(?P<year_id>\d+)/(?P<month_id>\d+)$', views.archive, name='archives'), 

更新:

Models.py

class Post(models.Model): 
title = models.CharField(max_length=100) 
body = models.TextField() 
created = models.DateTimeField(auto_now_add=True) 
url = models.URLField(null=True, blank=True) 
video = models.FileField(upload_to = 'video', verbose_name = 'Video', null=True, blank=True) 
picture = models.ImageField(upload_to = 'post', verbose_name = 'Picture') 
tags = TaggableManager() 

def __unicode__(self): 
    return self.title 

模板

<h3>Archivo</h3> 
     <p> 
     {% for month in months %} 
     {% ifchanged month.0 %} {{ month.0 }} <br /> {% endifchanged %} 
     <a href="/blog/archives/{{month.0}}/{{month.1}}">{{ month.2 }}</a> <br /> 
     {% endfor %} 
     </p> 

更新2:錯誤

usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response 
      response = middleware_method(request, response) ... 
/usr/local/lib/python2.7/dist-packages/django/middleware/common.py in process_response 
    if response.status_code == 404: ... 


Request Method: GET 
Request URL: http://127.0.0.1:8000/blog/archives/2014/1 
Django Version: 1.5 
Exception Type: AttributeError 
Exception Value:  
'tuple' object has no attribute 'status_code' 
Exception Location: /usr/local/lib/python2.7/dist-packages/django/middleware/common.py in process_response, line 106 
Python Executable: /usr/bin/python 
Python Version: 2.7.3 
Python Path:  
['/home/fernando/develop/blogmanage', 
'/usr/local/lib/python2.7/dist-packages/django_mptt-0.6.0-py2.7.egg', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-linux2', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PIL', 
'/usr/lib/python2.7/dist-packages/gst-0.10', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', 
'/usr/lib/pymodules/python2.7', 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client', 
'/usr/lib/python2.7/dist-packages/ubuntuone-client', 
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', 
'/usr/lib/python2.7/dist-packages/ubuntuone-couch', 
'/usr/lib/python2.7/dist-packages/ubuntuone-installer', 
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol'] 
Server time: Wed, 29 Jan 2014 21:09:56 +0100 
+5

URL捕獲變量「year_id」,但您的視圖採用參數「year」。 – RemcoGerlich

+0

沒錯!現在我得到一個新的錯誤。 – Amonshy

回答

7

也有一些是不對您的參數名稱:

def archive(request, year, month): 

通過year_idmonth_id更換yearmonth,它應該工作。

編輯:

你的第二個錯誤,並據此this question,您archive()視圖不返回正確的響應。

這裏是你的代碼,固定:

from django.shortcuts import render_to_response 

def archive(request, year_id, month_id): 
    posts = Post.objects.filter(created__year=year_id, created__month=month_id) 
    context = {'PostList': posts, 'Months': mkmonth_lst()} 

    # the error was here 
    return render_to_response('archives.html', context) 

編輯2:

您的模板無法通過months迭代,因爲變種不存在背景:

context = {'PostList': posts, 'Months': mkmonth_lst()} # Not correct 
context = {'postList': posts, 'months': mkmonth_lst()} # Correct 

待辦事項你看到了區別?在第一個變量名稱(「Months」)中使用大寫字母,而呈現的大小寫敏感的模板查找小寫字母變量(「months」)。

+0

沒錯!現在錯誤是:'元組'對象沒有屬性'status_code' – Amonshy

+0

請粘貼整個堆棧跟蹤和/或它發生的代碼=) –

+0

粘貼錯誤 – Amonshy

相關問題