在Django中,如何将参数从URL传递到VirtualDownloadView中的get_file()?

时间:2015-09-16 21:26:17

标签: python django django-views

以下是我的Django项目的一部分。

我已配置以下网址。

urlpatterns = patterns(
    url(r'^download/(?P<job>[0-9]+)/$', DownloadJobResults.as_view(), name='dm_download')
)

以下是DownloadJobResults类。

class DownloadJobResults(VirtualDownloadView):

    def get_file(self, job):

        print "Job is {}".format(job)

        file = StringIO.StringIO()
        file.write("test 1,2,3,4\n")
        file.write("test 5,6,7,8\n")
        file.seek(0)
        return VirtualFile(file, name='sweet.txt')

我最终会使用job参数来查找作业数据并使用它返回一个文件。但我无法通过参数传递。当我访问该URL时,我得到:

  

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

这是完整的堆栈跟踪。

Traceback: File "/Users/michael/app/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/michael/app/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs) File "/Users/michael/app/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs) File "/Users/michael/app/lib/python2.7/site-packages/django_downloadview/views/base.py" in get
  170.         return self.render_to_response() File "/Users/michael/app/lib/python2.7/site-packages/django_downloadview/views/base.py" in render_to_response
  154.             self.file_instance = self.get_file()

Exception Type: TypeError at /dm/download/5/ Exception Value: get_file() takes exactly 2 arguments (1 given)

1 个答案:

答案 0 :(得分:0)

我发现了问题。基本上,Django将那些命名的URL参数放在kwargs中。

If yes, indicates that the group created is a system group.