1

I'm using Django-countries and I don't overcome to display country flags beside each country in my Django Form or in any templates.

I display well the country list, I set correctly the flags folder in my static folder. But impossible to display flags.

In my model I have :

PaysNaissance = CountryField(blank_label='Sélectionner un pays', verbose_name='Pays de naissance')

Then, my form displays countries like this :

enter image description here

As you can see, none flags appears in my list. Do you have any idea about this issue ?

Settings are well-configured, and in my template I have :

<table style="width:100%">
            <tbody>
                <p></p>
            <tr>
                <th> Informations sur l'individu </th>
                <th> Données renseignées </th>
            </tr>
            <tr>
                <td>Pays de Naissance</td>
                <td>{{personne.PaysNaissance}}</td>
            </tr>
            </tbody>
 </table>

It displays : FR But If I want to display the flag, I set {{personne.PaysNaissance.flag}} and I have /static/flags/fr.gif.

So in order to display the flag I have to write : <td><img src='{{personne.PaysNaissance.flag.url}}'/></td>

But it doesn't seem to work.

In my form part :

<form class = "form" method='POST' action='' enctype="multipart/form-data"> {% csrf_token %} <br></br>

        {{ form.as_p }} 
        {{birthday|date:"%d/%m/%Y" }} 
        <input type="submit" onclick="return confirm('Valider le formulaire ?')" /> 
    </form>
Essex
  • 6,042
  • 11
  • 67
  • 139

1 Answers1

4

You need to write :

<td><img src='{{personne.PaysNaissance.flag}}'/></td>

Because {{ personne.PaysNaissance.flag }} itself contains the flag url. You don't need to write flag.url extra.

One more thing can be done :

Use :

<link rel="stylesheet" href="{% static 'flags/sprite.css' %}">


<td><i class="{{ personne.PaysNaissance.flag_css }}"></i></td>

This will work.

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
  • The first step works pretty well ! Just one question : If I want to display flags in my django list form, How I could do that ? – Essex Sep 18 '17 at 11:14
  • @Deadpool Can you show me the rendered output of HTML form?? The same way should happen for form also. – Prakhar Trivedi Sep 18 '17 at 11:17
  • Yes sure, you have an example in my question about my rendering output HTML form. I don't know If I have to add something like this : `{{ form.PaysNaissance.flag}}` – Essex Sep 18 '17 at 11:19
  • @Deadpool The {{ form.as_p }} is a model form right?? For that, you need to extend the country name with ints flag also. In the forms.py file. – Prakhar Trivedi Sep 18 '17 at 11:21
  • You're right ;) Ok I will try some things in my forms.py file ! Thank you – Essex Sep 18 '17 at 11:25
  • If I want to extend my django form in order to add flags, I have to add `def __init__` ? – Essex Sep 18 '17 at 11:54
  • @Deadpool Not necessarily. You can also set this at the time of Field creation. Please refer https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#a-full-example – Prakhar Trivedi Sep 18 '17 at 11:56
  • Hum a bit obscur for me but I will try to understand what I have to modify exactly in order to display Country flags ;) – Essex Sep 18 '17 at 12:06