encode / django-rest-framework

Web APIs for Django. 🎸
https://www.django-rest-framework.org
Other
28.45k stars 6.84k forks source link

reverse exception (dictionary update sequence element #0 has length 1; 2 is required) #1295

Closed almalki closed 10 years ago

almalki commented 10 years ago

I got this exception using DRF reverse, i am not sure if it is something wrong with DRF, Django or my setup. Basically what I am doing is

reverse('print_request_list', request=request, format=format)

in my root view for browsable API, where 'print_request_list' is a view:

url(r'^print/$', PdfPrinter.as_view(), name='print_request_list'),
Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/api/v1/

Django Version: 1.6.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'api',
 'rest_framework',
 'rest_framework_swagger')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback:
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  399.             response = self.handle_exception(exc)
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  396.             response = handler(request, *args, **kwargs)
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/rest_framework/decorators.py" in handler
  49.             return func(*args, **kwargs)
File "/home/abdulaziz/Dropbox/dev/print_it/api/views.py" in api_root
  92.         'print': reverse('print_request_list', request=request, format=format),
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/rest_framework/reverse.py" in reverse
  17.     url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
  509.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  387.         possibilities = self.reverse_dict.getlist(lookup_view)
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse_dict
  296.             self._populate()
File "/home/abdulaziz/dev/virtualenvs/print_it/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _populate
  278.                             lookups.appendlist(name, (new_matches, p_pattern + pat, dict(defaults, **pattern.default_kwargs)))

Exception Type: ValueError at /api/v1/
Exception Value: dictionary update sequence element #0 has length 1; 2 is required
tomchristie commented 10 years ago

The reverse function hasn't changed in 10 months and I've not seen any other issues raised, so on balance it seems most likely that this is a combination of an error in your URLconf and an unhelpful error message from Django's URL resolver as a result. (The exception is being raised by Django's reverse function)

I'd suggest narrowing this down by simply calling Django's reverse function instead, since that's where the exception is being raised...

from django.core.urlresolvers import reverse

reverse('print_request_list', kwargs={'format': format})

Don't know if this helps any? http://stackoverflow.com/questions/17610732/error-dictionary-update-sequence-element-0-has-length-1-2-is-required-on-dj

tomchristie commented 10 years ago

Because the exception is being raised by Django's reverse function I'm going to close this off for now, but I would still be interested in hearing the root cause once you've debugged this. Also, having said that, if you do some more digging and you still think this is a REST framework specific issue feel free to reopen the ticket and add further detail.

almalki commented 10 years ago

The stackoverflow link was really helpful, the problem was a missing "name" for the view name karg in urlconf. It was not the view that reverse was looking for, it was another view. I dont know what I was thinking when I raised this bug report without doing research, it was late night and was frustrated by lack support for different serializers for request and response..(I know it can be done with APIView, but in that case we lose the Browsable API) Sorry for the false alarm and keep up the good work..

tomchristie commented 10 years ago

no problem, glad it's resolved. :)