Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reverse() argument after ** must be a mapping

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
like image 609
Armance Avatar asked Mar 26 '13 16:03

Armance


Video Answer


2 Answers

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.

like image 75
FSp Avatar answered Oct 04 '22 03:10

FSp


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}))
like image 45
Weilory Avatar answered Oct 04 '22 04:10

Weilory