Home > database >  Button for selection data of database from django app
Button for selection data of database from django app

Time:04-12

enter image description hereI have a django app and database I need to create a button in my index.html , you click on it and can choose wich country you want to select but i have a probleme my button is like in the picture I have all the names in the same line and i have words .. who can help me ?

I created models:

class Country(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=1, blank=True, null=True)

    class Meta:
        db_table = 'country'

    def __str__(self):
        return f'{self.name}'

my index.html



            <form method="POST">
                <div >
                <div >
                    <div >
                        <span > <i ></i> </span>
                     </div>
                <select name="start_time" id="id_start_time" >
                      <option>{{name}}</option>
                </select>
                </div> <!-- input-group.// -->
                </div> <!-- form-group// -->
                <div >
                <div >
                                

CodePudding user response:

I think you are directly passing the query set in view and using it as .

You have to loop (using Django template language) through the query set in html like this:

<form method="POST">
                <div >
                <div >
                    <div >
                        <span > <i ></i> </span>
                     </div>
                <select name="start_time" id="id_start_time" >
                      {% for each_country in name %}
                          <option value="{{each_country.name}}">{{each_country.name}}</option>
                      {% endfor %}
                </select>
                </div> <!-- input-group.// -->
                </div> <!-- form-group// -->
                <div >
                <div >
                        

Notice that I have added for loop using Django template tags.

  • Related