Yeah, that could have been a greate solution :) The problem is that
there is a huge amount of key-value sets to replace, and i don't want
to replace "-" in the blog posts (Tumblr is a blogging service). That
would result in a pretty complex regular expression.

What I did instead to solve this was to make a template filter.
Syntax for using this is {{ post|tumblr_var:"regular-title" }}, and it
will print post["regulat-title"].

The code in the filter is simple:

from django import template

register = template.Library()

def tumblr_var(value, key):
    return value[key]

register.filter("tumblr_var", tumblr_var)

Thanks for your comments :)

On May 10, 12:17 pm, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:
> On May 10, 1:58 am, Danne <pingvingr...@gmail.com> wrote:
>
> > I'm using the Tumblr (http://www.tumblr.com) REST Api, and keys in
> > returned json contains the character "-", like "post": {"regular-
> > title": "asdf" .....}. I'm having trouble printing this in the django
> > templates since regular-title is an invalid variable name. I've tried
> > stuff like: post["regular-title"], post[regular-title], post.regular-
> > title and so on, and searched around the docs and mailing lists, but
> > haven't found any answers.
>
> > Is there a way to print template variables with "-" in their names?
>
> Django templates don't use brackets in dictionary lookups, so
> post.regular-title would be the correct syntax.
>
> How are you getting the data from Tumblr into the template? If the API
> is returning JSON, at some point (before you parse it) it's just a
> string, so you could do x.replace('regular-title', 'regular_title') to
> convert the keys into valid Python variables, if that is indeed the
> cause of your problem.
>
> It would probably help if you posted your view code.
> --
> DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to