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

addict outputs {'__unicode__': {}} rather than empty string when used in django templates. #54

Closed
AncientSwordRage opened this issue May 22, 2015 · 6 comments

Comments

@AncientSwordRage
Copy link

Here is a rough idea of the code I'm running:

from addict import Dict
def get_dictionary(self, orig_dict):
    new_dict = Dict({'default_key':'default_value'})
    if(orig_dict["code"]==0):
        new_dict.other_key = "Double Quoted String"
    elif(orig_dict["code"]==1):
        new_dict.other_key = "Different Double Quoted String"
    return new_dict

and eventually when I use it with Django's render_to_string("xml_template.xml",dictionary=context_dictionary) into this template:

<ParentTag>
    <MyFirstTag>{{new_dict.default_key}}</MyFirstTag>
    <MySecondTag>{{new_dict.other_key}}</MySecondTag>
</ParentTag>

And it renders as:

<ParentTag>
    <MyFirstTag>default_value</MyFirstTag>
    <MySecondTag>{&#39;__unicode__&#39;: {}</MySecondTag>
</ParentTag>

I think that (for some reason) addict is returning an empty dictionary, but the output is being mangled. I know the docs say you can use to_dict() but it implies you don't need to.

Any idea why this happens?

@mewwts
Copy link
Owner

mewwts commented May 22, 2015

Hi @AncientSwordRage,
That looks really weird! Could you tell me what you get when you do
<MySecondTag>{{new_dict['other_key']}}</MySecondTag>?

Thanks for submitting this issue! 👍

@mewwts
Copy link
Owner

mewwts commented Jun 22, 2015

@AncientSwordRage Any update on this?

@AncientSwordRage
Copy link
Author

I've not named to look into this, but I've not forgotten.

On Mon, 22 Jun 2015 8:29 AM Mats Julian Olsen notifications@github.com
wrote:

@AncientSwordRage https://github.com/AncientSwordRage Any update on
this?


Reply to this email directly or view it on GitHub
#54 (comment).

@mewwts
Copy link
Owner

mewwts commented Aug 9, 2015

I'm closing this for now, as it looks a bit lika a django escape sequence.

@mewwts mewwts closed this as completed Aug 9, 2015
@ckot
Copy link

ckot commented Oct 30, 2016

I can verify that the passing of the .to_dict() value of the Dict object fixes the &39;unicode&39; issue, but the dict still displays all keys wrapped with &39;, This happens with any dict, and the problem is not specific to addict.

The solution is to use the |safe template filter when you are either displaying entire dictionaries or keys which themselves have nested dictionaries (probably only useful for dumping data-structures during development anyway). example usage: {{ mydict|safe }} or {{ mydict.somekey|safe }}

@mewwts
Copy link
Owner

mewwts commented Oct 30, 2016

Thanks for clarifying @ckot 👍

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

3 participants