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

 心如止水向北飞2012_737 发布于 2023-01-30 15:16

我正在尝试创建一个存档,所以我将参数年份和月份传递给视图.

但是,我在下面的代码中遇到错误,我无法弄清楚它的含义以及如何解决它:

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\d+)/(?P\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

模板

Archivo

{% for month in months %} {% ifchanged month.0 %} {{ month.0 }}
{% endifchanged %} {{ month.2 }}
{% endfor %}

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

Eliot Berrio.. 7

参数名称有问题:

 def archive(request, year, month):

替换yearmonthby year_idmonth_id它应该工作.

编辑:

对于您的第二个错误,以及相应的问题,您的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因为var在上下文中不存在:

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

你看得到差别吗 ?您在第一个变量名称("Months")中使用大写字母,而呈现的模板(区分大小写)查找小写变量("months").

1 个回答
  • 参数名称有问题:

     def archive(request, year, month):
    

    替换yearmonthby year_idmonth_id它应该工作.

    编辑:

    对于您的第二个错误,以及相应的问题,您的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因为var在上下文中不存在:

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

    你看得到差别吗 ?您在第一个变量名称("Months")中使用大写字母,而呈现的模板(区分大小写)查找小写变量("months").

    2023-01-30 15:17 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有