14

我有一个在提交时不起作用的题词表格

我收到此错误:

** 之后的 reverse() 参数必须是映射,而不是 str

这是我的观点:

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)

我的表格:

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

似乎触发错误的是返回语句中的**inscription_config

但我不知道为什么,从昨天开始我一直在寻找

编辑

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
4

2 回答 2

19

我尝试回答,因为我遇到了同样的问题,但没有在网上找到答案。

我认为这个问题的根源是错误的get_absolute_url(...)方法。例如,如果你这样写:

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

然后它引发异常reverse() argument after ** must be a mapping, not str。修复它删除@models.permalink装饰器,如下:

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

或者保留装饰器并修改主体,如下所示:

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

不过,我认为后者已被弃用。

于 2014-09-04T18:07:44.470 回答
1

顽皮的逗号

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

它应该是

return redirect(reverse_lazy('team-detail', kwargs={'pk': team.pk}))
于 2021-12-11T06:30:23.793 回答