Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError unhashable type: 'dict' #180

Closed
bxm156 opened this issue May 12, 2013 · 18 comments
Closed

TypeError unhashable type: 'dict' #180

bxm156 opened this issue May 12, 2013 · 18 comments

Comments

@bxm156
Copy link

bxm156 commented May 12, 2013

After upgrading from 1.2.7 to 1.2.8, I receive an error message when calling:

{% crispy form %}

crispy_forms/base.py in enter, line 25

Traceback:
File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response

  1.                     response = callback(request, _callback_args, *_callback_kwargs)
    
    File "/Users/Bryan/DjangoWorkspace/BioCrowd/BioCrowd/apps/registration/views.py" in register
  2. return render_to_response('register.djhtml', context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/shortcuts/init.py" in render_to_response
  3. return HttpResponse(loader.render_to_string(_args, *_kwargs), **httpresponse_kwargs)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  4.     return t.render(Context(dictionary))
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/base.py" in render
  5.         return self._render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  6. return self.nodelist.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/base.py" in render
  7.             bit = self.render_node(node, context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/debug.py" in render_node
  8.         return node.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  9.     return compiled_parent._render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  10. return self.nodelist.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/base.py" in render
  11.             bit = self.render_node(node, context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/debug.py" in render_node
  12.         return node.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  13.     return compiled_parent._render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  14. return self.nodelist.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/base.py" in render
  15.             bit = self.render_node(node, context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/debug.py" in render_node
  16.         return node.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  17.         result = block.nodelist.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/base.py" in render
  18.             bit = self.render_node(node, context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/debug.py" in render_node
  19.         return node.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  20.         result = block.nodelist.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/base.py" in render
  21.             bit = self.render_node(node, context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/debug.py" in render_node
  22.         return node.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  23.         result = block.nodelist.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/base.py" in render
  24.             bit = self.render_node(node, context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/django/template/debug.py" in render_node
  25.         return node.render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/crispy_forms/templatetags/crispy_forms_tags.py" in render
  26.     c = self.get_render(context)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/crispy_forms/templatetags/crispy_forms_tags.py" in get_render
  27.             actual_form.form_html = helper.render_layout(actual_form, node_context, template_pack=self.template_pack)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/crispy_forms/helper.py" in render_layout
  28.                               template_pack=template_pack)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/crispy_forms/layout.py" in render
  29.                              context, template_pack=template_pack)
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/crispy_forms/utils.py" in render_field
  30. with KeepContext(context):
    
    File "/Users/Bryan/PythonEnvs/BioCrowd/lib/python2.7/site-packages/crispy_forms/base.py" in enter
  31.     self.old_set_keys = set(from_iterable(self.context.dicts))
    

Exception Type: TypeError at /account/register/
Exception Value: unhashable type: 'dict'

@maraujop
Copy link
Contributor

Hi Bryan,

Thanks for reporting this. KeepContext is giving me a lot of trouble to get right. A couple of releases were done to fix issues in corner cases and I actually thought we got it right last time.

One thing that would be great is to see is what self.context.dicts holds, could you go into crispy-forms code, base.py module and put a breakpoint or a simple print there and post it here? That would be of great help.

It looks like some app might be causing trouble, I've tried to reproduce it installing django-registration here, but that's not the one. From your traceback I'm guessing you are rendering RegistrationForm using a helper and a layout, is that right? Maybe, could you post what other INSTALLED_APPS you have?

Thanks, cheers
Miguel

@bxm156
Copy link
Author

bxm156 commented May 12, 2013

I can't print self.context.dicts because it doesn't exist, but I can print self.context

I did print self.context in base.py KeepContext enter and got:

{'False': False, 'None': None, 'True': True}, [{'False': False, 'None': None, 'True': True}, {u'csrf_token': <django.utils.functional.__proxy__ object at 0x108e05550>}, {'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x108e05690>, 'user': <django.utils.functional.SimpleLazyObject object at 0x1087b1090>}, {u'debug': True, u'sql_queries': [{u'time': u'0.001', u'sql': u'SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."session_key" = vdul7w3ecg8lptwixq2arpcuoth9qcuh  AND "django_session"."expire_date" > 2013-05-12 21:53:18.545559 )'}]}, {u'LANGUAGES': (('af', 'Afrikaans'), ('ar', 'Arabic'), ('az', 'Azerbaijani'), ('bg', 'Bulgarian'), ('be', 'Belarusian'), ('bn', 'Bengali'), ('br', 'Breton'), ('bs', 'Bosnian'), ('ca', 'Catalan'), ('cs', 'Czech'), ('cy', 'Welsh'), ('da', 'Danish'), ('de', 'German'), ('el', 'Greek'), ('en', 'English'), ('en-gb', 'British English'), ('eo', 'Esperanto'), ('es', 'Spanish'), ('es-ar', 'Argentinian Spanish'), ('es-mx', 'Mexican Spanish'), ('es-ni', 'Nicaraguan Spanish'), ('es-ve', 'Venezuelan Spanish'), ('et', 'Estonian'), ('eu', 'Basque'), ('fa', 'Persian'), ('fi', 'Finnish'), ('fr', 'French'), ('fy-nl', 'Frisian'), ('ga', 'Irish'), ('gl', 'Galician'), ('he', 'Hebrew'), ('hi', 'Hindi'), ('hr', 'Croatian'), ('hu', 'Hungarian'), ('ia', 'Interlingua'), ('id', 'Indonesian'), ('is', 'Icelandic'), ('it', 'Italian'), ('ja', 'Japanese'), ('ka', 'Georgian'), ('kk', 'Kazakh'), ('km', 'Khmer'), ('kn', 'Kannada'), ('ko', 'Korean'), ('lb', 'Luxembourgish'), ('lt', 'Lithuanian'), ('lv', 'Latvian'), ('mk', 'Macedonian'), ('ml', 'Malayalam'), ('mn', 'Mongolian'), ('nb', 'Norwegian Bokmal'), ('ne', 'Nepali'), ('nl', 'Dutch'), ('nn', 'Norwegian Nynorsk'), ('pa', 'Punjabi'), ('pl', 'Polish'), ('pt', 'Portuguese'), ('pt-br', 'Brazilian Portuguese'), ('ro', 'Romanian'), ('ru', 'Russian'), ('sk', 'Slovak'), ('sl', 'Slovenian'), ('sq', 'Albanian'), ('sr', 'Serbian'), ('sr-latn', 'Serbian Latin'), ('sv', 'Swedish'), ('sw', 'Swahili'), ('ta', 'Tamil'), ('te', 'Telugu'), ('th', 'Thai'), ('tr', 'Turkish'), ('tt', 'Tatar'), ('udm', 'Udmurt'), ('uk', 'Ukrainian'), ('ur', 'Urdu'), ('vi', 'Vietnamese'), ('zh-cn', 'Simplified Chinese'), ('zh-tw', 'Traditional Chinese')), u'LANGUAGE_BIDI': False, u'LANGUAGE_CODE': 'en-us'}, {u'MEDIA_URL': ''}, {u'STATIC_URL': '/static/'}, {u'TIME_ZONE': 'EDT'}, {'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x1087b1950>}, {'SITE_TITLE': 'BioCrowd'}, {'form': <BioCrowd.apps.registration.forms.UserForm object at 0x10885e2d0>}], {'block': <Block Node: body. Contents: [<Text Node: '
<div class="navbar navba'>, <Block Node: project_name. Contents: [<Variable Node: SITE_TITLE>]>, <Text Node: '</a>
            <div class="nav-c'>, <IfNode>, <Text Node: '
                </p>
                <ul class="'>, <Block Node: container. Contents: [<Text Node: '
    <div class="row-fluid">'>, <Block Node: content. Contents: [<Text Node: '
            <h1>Bootstrap starter'>]>, <Text Node: '
        </div>
    </div>
    '>]>, <Text Node: '
    '>, <django.template.loader_tags.ConstantIncludeNode object at 0x108e15dd0>, <Text Node: '
</div>
'>]>}, {'block': <Block Node: container. Contents: [<Text Node: '
    <div class="row-fluid">'>, <Block Node: content. Contents: [<Text Node: '
            <h1>Bootstrap starter'>]>, <Text Node: '
        </div>
    </div>
    '>]>}, {'block': <Block Node: content. Contents: [<Text Node: '
<h1>Sign up</h1>
<div cl'>, <crispy_forms.templatetags.crispy_forms_tags.CrispyFormNode object at 0x108e128d0>, <Text Node: '
</div>
'>]>}, {'flat_attrs': u' action="/account/register/" id="id-registration"', 'inputs': [], 'csrf_token': <django.utils.functional.__proxy__ object at 0x108e05550>, 'form_id': 'id-registration', 'form_error_title': None, 'form_action': '/account/register/', 'error_text_inline': True, 'html5_required': False, 'help_text_inline': False, 'formset_error_title': None, 'form_style': '', 'form_method': 'post', 'form_show_errors': True, 'is_formset': False, 'form_tag': True, 'attrs': {'action': '/account/register/', 'id': 'id-registration'}, 'form_attrs': {'action': '/account/register/', 'id': 'id-registration'}, 'form_class': ''}]

My App list:
'compressor',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'BioCrowd.apps.accounts',
'BioCrowd.apps.registration',
'BioCrowd.apps.login',
'BioCrowd.apps.pages',
'BioCrowd.apps.jobs',
'BioCrowd.apps.universities',
'pycrowd',
'pycrowd.workers',
'pycrowd.query',
'pycrowd.jobs',
'pycrowd.hits',
'crispy_forms',
'debug_toolbar',
# Uncomment the next line to enable the admin:
'django.contrib.admin',

Things relating to BioCrowd or pycrowd are might stuff, which should't be causing a problem since most of them are empty placeholders.

The form is:

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    confirm_password = forms.CharField(widget=forms.PasswordInput)
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', 'password', 'confirm_password', )

    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-registration'
        #self.helper.form_class = 'form-horizontal'
        self.helper.form_method = 'post'
        self.helper.form_action = '/account/register/'
        #self.helper.add_input(Submit('submit', 'Sign up'))
        self.helper.layout = Layout(
            Fieldset(
                'Sign up',
                'first_name',
                'last_name',
                'email',
                'password',
                'confirm_password'
            ),
            FormActions(
                Submit('submit', 'Signup', css_class="btn-large")
            )
        )
        self.helper.filter(basestring, greedy=True).wrap(Field, css_class="input-xlarge")

    def clean_confirm_password(self):
        password = self.cleaned_data.get('password')
        confirm_password = self.cleaned_data.get('confirm_password')

        if not confirm_password:
            raise forms.ValidationError("You must confirm your password")
        if password != confirm_password:
            raise forms.ValidationError("Your passwords do not match")
        return confirm_password

    def save(self):
        new_user = User.objects.create_user(
            email=self.cleaned_data['email'],
            password=self.cleaned_data.get('password'),
            first_name=self.cleaned_data['first_name'],
            last_name=self.cleaned_data['last_name'],
            is_active=True,
        )
        new_user.save()
        return new_user

@maraujop
Copy link
Contributor

I've been some days trying to reproduce the bug without luck. I've tried everything: different Django/Python versions, installing some of the apps you are using and checking if they somehow affect context variable, etc. Reproducing your same UserForm with django-registration.

It is very weird that your context doesn't have a dicts attribute. There a couple extra things that could help me debug this issue:

  • One would be sending me the project zipped to my email, so I can debug it myself. This could probably not be a possibility, due to NDAs or propietary code. Of course, you have my compromise for keeping all private and deleting everything right after figuring out what's going on.
  • What type of variable is self.context: executing type(self.context). Maybe it's not an official Django context?!

Anyway, this needs to be patched, but I rather know what I'm patching.

Cheers,
Miguel

@EstebanMDQ
Copy link

this happened to me when using RequestContext and passing it as context to render_to_template instead of context_instance

def my_view(request):
   context = RequestContext(request)
   #do something here and finally
   return render_to_response('my_template.html', {}, context_instance=context)

@maraujop
Copy link
Contributor

HI Esteban,

Thanks for adding more information to this. I'm not sure I know a render_to_template function in Django, maybe you meant render_to_response ? I've just tried this with a render_to_response and nothing breaks :/ What exact version of Django were you using when this happened?

Thanks, cheers
Miguel

@EstebanMDQ
Copy link

you're right!! my bad, this happens with render_to_response using both Django 1.5 and 1.4 and de dev version of crispy forms

@maraujop
Copy link
Contributor

No problem :)

I've tried this right now in a test project and nothing. any chance you can send me your project zipped? I hate when I cannot reproduce bugs...

Cheers,
Miguel

@EstebanMDQ
Copy link

I can't send the project, but I'll try to create a working example later. anyway with this workaround I don't have any issues

@maraujop
Copy link
Contributor

Thanks Esteban, that would be extremely helpful!

Cheers,
Miguel

@EstebanMDQ
Copy link

you can get the example here

http://informalthinkers.com/example.tar.gz

access /form1 and /form2 basically the same code, the second one isn't working

@onjin
Copy link

onjin commented May 17, 2013

I have same issue with

  • django 1.4.5
  • django-crispy-forms 1.2.8
  • python 2.7

My view returns

@bxm156
Copy link
Author

bxm156 commented May 17, 2013

Mine uses render_to_response as well.

maraujop added a commit that referenced this issue May 18, 2013
This fixes issue #180 and adds a test for avoiding regressions
@maraujop
Copy link
Contributor

Ok, thanks to the examples you've submitted, I've been able to reproduce the issue. Apparently render_to_response handles context differently. I've just pushed a patch to dev branch that I'm quite confident fixes the issue. Could anyone please confirm this is correct?

Thanks, cheers
Miguel

@EstebanMDQ
Copy link

Will do later today

On Saturday, May 18, 2013, Miguel Araujo wrote:

Ok thanks to the examples you've submitted, I've been able to reproduce
the issue. Apparently render_to_response handles context differently.
I've just pushed a patch to dev branch that I'm quite confident fixes the
issue. Could anyone please confirm this is correct?

Thanks, cheers
Miguel


Reply to this email directly or view it on GitHubhttps://github.com//issues/180#issuecomment-18098715
.

Esteban Soler
esteban@daemoniclabs.com
follow me @EstebanSoler
dabble.it/esteban
+54 9 223 581 4112
Mar del Plata

@josharenson
Copy link

I can confirm that as of commit 472889f this issue no longer exists for me. Thanks you very much!

@maraujop
Copy link
Contributor

Thanks a lot @josharenson for confirming it. This is going to be released in version 1.3.0 as soon as possible.

Cheers,
Miguel

@johnraz
Copy link

johnraz commented Jun 4, 2013

Same issue here, and I can confirm that the dev branch fixes it.

@maraujop
Copy link
Contributor

Version 1.3.0 released :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants