简体   繁体   English

Django 得到了一个意外的关键字参数

[英]Django got an unexpected keyword argument

I'm trying to create an archive so I pass to the view the arguments year and month.我正在尝试创建一个存档,因此我将参数年和月传递给视图。

However, I get an error with the code below and I can't figure out what it means and how to solve it:但是,下面的代码出现错误,我无法弄清楚它的含义以及如何解决它:

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

What can be wrong?有什么问题?

Views.py视图.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网址.py

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

Update:更新:

Models.py模型.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

Template模板

<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>

Update 2: error更新 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

There is something wrong with your parameters names :您的参数名称有问题:

 def archive(request, year, month):

Replace year and month by year_id and month_id and it should work.year_idmonth_id替换yearmonth ,它应该可以工作。

EDIT:编辑:

For your second error, and accordingly to this question , your archive() view does not return a proper response.对于您的第二个错误,相应于这个问题,您的archive()视图没有返回正确的响应。

Here is your code, fixed :这是您的代码,已修复:

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)

Edit 2 :编辑2:

Your template can't iterate through months because the var does not exist in context:您的模板无法迭代months因为上下文中不存在 var:

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

Do you see the difference ?你看得到差别吗 ? You use caps for your variables names ("Months") in the first one, while the template rendered, which is case-sensitive, looks for a lower case variable ("months").您在第一个变量名称(“月份”)中使用大写字母,而呈现的模板区分大小写,查找小写变量(“月份”)。

In my case, it was a typo for using : instead of = in the model definition.就我而言,在模型定义中使用:而不是=是一个错字。

Eg it was:例如它是:

class Workspace(models.Model):
    title: models.CharField(max_length=200)
    description: models.CharField(max_length=2000)
...

But should be:但应该是:

class Workspace(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=2000)
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM