search()只需要2个参数(给定1个)

时间:2013-08-03 14:32:52

标签: python django django-models django-forms django-templates

views.py

def search(request,csv_export):
    if request.method == 'POST':
        reports = Report.objects.filter(user__in=user_list).order_by('-created_date_time', '-id')
    ''''' 
    some code
    ''''''
    if csv_export:  
        response = HttpResponse(mimetype='text/csv')
        filename = "%s-%s.csv" % ('incident-reporter', datetime.datetime.now().strftime('%Y-%m-%d'))
        response['Content-Disposition'] = 'attachment; filename=%s' % filename
        writer = UnicodeWriter(response, encoding='utf-8')
        writer.writerow(headerrow)
        for writerow in report_list:
            writer.writerow(writerow)
        return response        
    return render(request,'search.html',{'report':report})  

urls.py

 urlpatterns = patterns(
     'incident.views',
     (r'^$', 'index'),
     (r'^search/$', 'search',{"csv_export": False}),
     (r'^csv_export/$', 'csv_export',{"csv_export": True}),
     (r'^what/$', 'what'),
     (r'^where/$', 'where'),
     (r'^when/$', 'when'),
     (r'^media/$', 'media'),
)

我收到此错误:

  

“/ report / search / search()中的TypeError恰好有2个参数(给定1个)”

见下面我的追溯:

Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

Exception Type: TypeError at /report/search/
Exception Value: search() takes exactly 2 arguments (1 given)

修改:urls.py已更新问题

2 个答案:

答案 0 :(得分:0)

问题可能源于其他地方(我认为在{% url %}电话中),但它应该很容易解决:

def search(request, csv_export=False):

然后在urls.py中:

(r'^search/$', 'search'),  # <- more elegant, isn't it?
(r'^csv_export/$', 'csv_export', {"csv_export": True}),

这为csv_export提供了一个defult值,如果它在没有参数的情况下被调用。

如果这不能解决您的问题,您应该发布有关您的架构的更多详细信息。

答案 1 :(得分:-4)

如果

{"csv_export": False}

是您调用搜索功能的方式,然后很明显。

搜索函数有两个参数:(request,csv_export)。您只发送一个参数(csv_export)

您的函数调用应如下所示:

 {"request": something, "csv_export": False}