0

I have in Django a view with an autocomplete search. In the logic of the script, I search for city name or I search by zip code. In the select form it works. Is it possible to display both at the same time?

here my form with the jquery :

<form  method="GET" action="{% url 'ville_select' name='name' %}" > 
         {% csrf_token %}
               <label for="villeselect">Ville</label>
        <input type="text"  name="name"  id="villeselect" >
        <button type="submit">Go</button>
    </form>  
$(function () {
            $("#villeselect").autocomplete({
                source: "{% url 'recherche' %}",
                minLength: 3
            });
        });
    </script>

the class for display result in views.py

class VilleSelect(ListView):
    model = VilleCountry    
    template_name = 'ville/ville_select.html'    
        
    def get_context_data(self,*arg,**kwargs):
        
        context = super(VilleSelect,self).get_context_data(*arg,**kwargs)
        query = self.request.GET.get('name')
        context['posts'] = VilleCountry.objects.filter(Q(name__startswith=query) | Q(zip_code__startswith=query) )   
               
        return context

currently i have this

enter image description here

and what i want

enter image description here

it is possible to make this.

thank

1 Answer 1

0

See if this works, Try adding __str__() function in your VilleCountry model

Class VilleCountry(models.Model):
    name = models.CharField(max_length=1000)
    zip_coide = models.CharField(max_length=1000)

    class Meta:
        db_table = "ville_country"

    def __str__(self):
        return f"{self.name} - {self.zip_code}"

This will ensure that this model will always return both name and zipcode

Sign up to request clarification or add additional context in comments.

1 Comment

I added in my model return f"{self.name} - {self.zip_code}" instead of return "self.name", unfortunately it does not change anything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.