-2

I would like to display the language(s) a user chose for his user profile. Everything is working I just can't display the full language name. So when I write {{user.userprofile.language}} the html Output is "English, Spanish, French" but when I write {{user.userprofile.language.0}} I get "en" instead of "English".

What I have right now:

<span>En</span>

<span>Fr</span>

<span>Sp</span>

What I would like to have:

<span>English</span>

<span>French</span>

<span>Spanish</span>

Anybody know how to display the full Value?

EDIT:

class UserProfile(models.Model):
    language = MultiSelectField(max_choices=3,choices=settings.LANGUAGES, default='en')

settings.py:

LANGUAGES = (
('en', _('English')),
('pt', _('Portuguese')),
('dt', _('Deutsch')),
('sp', _('Spanish')),
('fr', _('French')),
('nl', _('Dutch')),
('pl', _('Polish')),
('au', _('Austrian')),
('ch', _('Schweizerisch')),
('hr', _('Kroatisch')),
('ru', _('Russian')),
)
alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
hansTheFranz
  • 2,511
  • 4
  • 19
  • 35
  • what is the full value? you are getting english? so? – Exprator May 31 '17 at 14:43
  • im NOT getting "English" I get "en" when I do {{user.userprofile.language.0}} but I Would like to get "English" instead. The User should see the full word not just two letters... – hansTheFranz May 31 '17 at 14:51
  • You haven't shown your models so we have no idea what `userprofile.language` is. – Alasdair May 31 '17 at 14:52
  • try {{user.userprofile.get_language_display}} – Exprator May 31 '17 at 14:53
  • @Exprator same as {{user.userprofile.language}} as soon as I write a number behind it it shows me just one letter. instead of the whole word. so {{user.userprofile.get_language_display.0}} gives me "E" instead of "English" – hansTheFranz May 31 '17 at 14:58
  • dont use 0, just till display – Exprator May 31 '17 at 14:59
  • But then he shows me 3 languages but I only want one. I think I will write some regex with js or split the three values at "," and add them manually in the template. Thanks anyway for trying :) – hansTheFranz May 31 '17 at 15:04

1 Answers1

1

It looks like you're using this django-multiselectfield package?

Reading the documentation, it looks like you should be doing something more along the lines of this:

{% for code, name in user.userprofile.language %}
    <span>{{ name }}</span>
{% endfor %}

Or perhaps:

{% for code, name in user.userprofile.language.choices %}
    <span>{{ name }}</span>
{% endfor %}

Do either of those solutions work for you?

If not, could you please confirm where the MultiSelectField in your model was imported from?

tdsymonds
  • 1,679
  • 1
  • 16
  • 26
  • `from multiselectfield import MultiSelectField` yes I use the package multi select but I think its more of a Django thing. Your answer did not work as well. The first code shows just one letter of each language. The second code shows nothing. I made it work with js. Took the string split it and append the word on the page when it loads. Thanks anyway for your help :) – hansTheFranz May 31 '17 at 23:15
  • Strange. I'm not too familiar with that package, so afraid I don't have a better suggestion if that didn't work. At least you got working with JS. – tdsymonds Jun 01 '17 at 07:45