reverse() argument after ** must be a mapping

19,250

Solution 1

I try to answer, since I had the same problem but didn't find an answer online.

I think the origin of this problem is a wrong get_absolute_url(...) method. For example, if you write it like this:

@models.permalink
def get_absolute_url(self):
    return reverse('my_named_url', kwargs={ "pk": self.pk })

Then it raises the exception reverse() argument after ** must be a mapping, not str. Fix it removing the @models.permalink decorator, as follows:

def get_absolute_url(self):
    return reverse('my_named_url', kwargs={ "pk": self.pk })

or alternatively keep the decorator and modify the body, as follows:

@models.permalink
def get_absolute_url(self):
    return ('my_named_url', (), { "pk": self.pk })

I think that the latter is deprecated, though.

Solution 2

Naughty comma

return redirect(reverse_lazy('team-detail', kwargs={'pk', team.pk}))

it should be

return redirect(reverse_lazy('team-detail', kwargs={'pk': team.pk}))
Share:
19,250

Related videos on Youtube

Armance
Author by

Armance

Backend developer

Updated on September 15, 2022

Comments

  • Armance
    Armance over 1 year

    I have an inscription form that doesnt work when submitting

    I get this error :

    reverse() argument after ** must be a mapping, not str

    This is my view :

    def inscription(request, seance_id):
        seance = get_object_or_404(Variant, id=seance_id)
        inscription_config = {'form_class': InscriptionForm,
                              'extra_context': {'seance': seance}}    
    
        return create_object(request, **inscription_config)
    

    My form :

    class InscriptionForm(forms.ModelForm):
        class Meta:
            model = Inscription
    
        def clean(self):
            cleaned_data = self.cleaned_data
            email = cleaned_data.get("mail")
            mail_confirmation = cleaned_data.get("mail_confirmation")
    
            if email != mail_confirmation:
                raise forms.ValidationError("Les deux adresses mails doivent correspondre")
    
            return cleaned_data
    

    Seems what triggering the error is **inscription_config in the return statement

    But I have no idea why, and I've been looking since yesterday

    EDIT

    Environment:
    
    Request Method: POST
    Request URL: http://127.0.0.1:8039/formations/inscription/1/
    Django Version: 1.2.5
    Python Version: 2.7.2
    Installed Applications:
    ['django.contrib.auth',
     'django.contrib.comments',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.sites',
     'django.contrib.messages',
     'django.contrib.admin',
     'ckeditor',
     'constance',
     'constance.backends.database',
     'custom_flatpages',
     'django_extensions',
     'django_mobile',
     'django_xmlrpc',
     'easy_thumbnails',
     'gestion_formations',
     'file_uploader',
     'less',
     'mptt',
     'contact',
     'newsletter',
     'pagination',
     'south',
     'sentry',
     'sentry.client',
     'indexer',
     'paging',
     'contentadmin',
     'gallerie']
    Installed Middleware:
    ('annoying.middlewares.StaticServe',
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'pagination.middleware.PaginationMiddleware')
    
    
    
    Traceback:
    
    File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
      100.                     response = callback(request, *callback_args, **callback_kwargs)
    File "/home/anass/projects/c139_fc_finance/fc_finance/gestion_formations/views/carts.py" in inscription
      24.                          form_class= InscriptionForm 
    File "/usr/local/lib/python2.7/dist-packages/django/views/generic/create_update.py" in create_object
      118.             return redirect(post_save_redirect, new_object)
    File "/usr/local/lib/python2.7/dist-packages/django/views/generic/create_update.py" in redirect
      65.         return HttpResponseRedirect(obj.get_absolute_url())
    File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py" in _curried
      55.         return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py" in get_absolute_url
      969.     return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self, *args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/__init__.py" in inner
      32.         return reverse(bits[0], None, *bits[1:3])
    File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in reverse
      351.             *args, **kwargs)))
    
    Exception Type: TypeError at /formations/inscription/1/
    Exception Value: reverse() argument after ** must be a mapping, not str
    
    • yantrab
      yantrab about 11 years
      Please provide the entire traceback. The code you show here doesn't look like it will cause the error you are talking about.
    • Daniel Roseman
      Daniel Roseman about 11 years
      This appears to be a problem with your get_absolute_url method. You should post that.
    • Armance
      Armance about 11 years
      @DanielRoseman If you can write an answer so I can accept it, and maybe explain how did you know the error came from get_absolute_url
  • AnonymousUser
    AnonymousUser over 2 years
    In my code return reverse('article-detail', kwargs=({'pk': self.post.pk}))
  • voneiden
    voneiden over 2 years
    Oops, I did it again.