Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django得到一个意外的关键字参数_Python_Django - Fatal编程技术网

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

Python Django得到一个意外的关键字参数,python,django,Python,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

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

但是,我在下面的代码中遇到了一个错误,我无法理解它的含义以及如何解决它:

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)
url.py

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

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

您的参数名称有问题:

 def archive(request, year, month):
年份
月份
替换为
年份id
月份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:

您的模板无法遍历
月份
,因为上下文中不存在var:

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

你看到区别了吗?第一个变量名(“月”)使用大写,而呈现的模板区分大小写,查找小写变量(“月”)。

在我的例子中,这是在模型定义中使用
而不是
=
的错误

例如:

class Workspace(models.Model):
    title: models.CharField(max_length=200)
    description: models.CharField(max_length=2000)
...
但应该是:

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

URL捕获了一个变量“year\u id”,但您的视图使用了参数“year”。没错!现在我又犯了一个错误,没错!现在的错误是:“tuple”对象没有属性“status\u code”请粘贴整个stacktrace和/或它发生的代码=)谢谢这解决了我的问题,但是现在web浏览器没有显示完整的模板,只加载了部分模板。我想错误位于
mkmonth\u lst()
。但是,我不理解您使用此函数的目标,因此我无法提供帮助,抱歉=/我的目标是创建存档。我遵循这个指导,但我有问题,以适应我的博客应用程序的存档视图。
class Workspace(models.Model):
    title: models.CharField(max_length=200)
    description: models.CharField(max_length=2000)
...
class Workspace(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=2000)
...