Gagandeep Singh
Gagandeep Singh

Reputation: 5879

Django error 'too many values to unpack' in objects.get(id)

I have url like this http://gagandeepdesk/2690/ which I am filtering with urls.py file and sending 2690 as one of the parameters to the function.

my urls.py filter looks like this,

url(r'^(\d+)/', 'scango.scanner.views.download'),

And that function looks like this,

def download(request, MediaId):
    media = Media.objects.get(id=int(MediaId))
    #print media
    return HttpResponse(FileIterWrapper(open(media.path)))

I am getting error 'too many values to unpack' on line

media = Media.objects.get(id=int(MediaId))

I also tried running this function standalone without request object and it is running perfectly fine. So, I am confused what I have done wrong here.

Environment:

Request Method: GET
Request URL: http://gagandeepdesk/2690/

Django Version: 1.3
Python Version: 2.7.1
Installed Applications:
['haystack',
 'scanner',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')

Traceback:

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\gagasing\Desktop\ideas\scango\..\scango\scanner\views.py" in download
  21.   media = Media.objects.get(id=int(MediaId))
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in get
  132.         return self.get_query_set().get(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in get
  343.         clone = self.filter(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in filter
  552.         return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  570.             clone.query.add_q(Q(*args, **kwargs))
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
  1170.                             can_reuse=used_aliases, force_having=force_having)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_filter
  1013.         arg, value = filter_expr

Exception Type: ValueError at /2690/
Exception Value: too many values to unpack

Upvotes: 1

Views: 1312

Answers (1)

Uku Loskit
Uku Loskit

Reputation: 42040

 (r'^(?P<MediaId>\d+)/$', 'scango.scanner.views.download')

Maybe try this instead?

This is an example of named groups.

In Python regular expressions, the syntax for named regular expression groups is (?Ppattern), where name is the name of the group and pattern is some pattern to match.

Upvotes: 4

Related Questions