Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Field in Django Python

Tags:

python

django

First, I have to say that is this my first application in Django. So my knowledge is still limited.

I have this home page where it shows all the data in my model. The model name is "Asset".

I am trying to have a search field inside the home page.

models.py

class Asset(models.Model):
    asset_desc = models.CharField(max_length=120, null=False)
    BEIRUT = 'Beirut'
    SAIDA = 'Saida'
    HALBA = "Halba"
    base_choice = ((SAIDA, "Saida"), (BEIRUT, "Beirut"), (HALBA, "Halba"))
    asset_base = models.CharField(max_length=120, null=False, choices=base_choice)
    created_date = models.DateField(auto_now_add=True)
    update_date = models.DateTimeField(auto_now=True)
    asset_user = models.CharField(max_length=120, blank=True)
    slug = models.SlugField()

    def save(self, *args, **kwargs):
        self.slug = slugify(self.asset_desc)
        super(Asset, self).save(*args, **kwargs)

    def __str__(self):
        return self.asset_desc

views.py

def search_asset(request):
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']
    assets = Asset.objects.filter(asset_desc__icontains=q)
    context = {'desc': assets}
    return render(request, 'home.html', context)

html for the search field:

    <form method="GET" class="navbar-form navbar-right">
      <input type="text" class="form-control" placeholder="Search..."id="search_box" name="q">

urls.py

url(r'^search/$', "asset.views.search_asset", name="home")

Please any help on why it is not showing the result. I am using Django 1.9.

like image 489
marwan h-sleiman Avatar asked Aug 25 '26 05:08

marwan h-sleiman


2 Answers

some corrections:

you dont need null=False for TextField() and CharField(), since they never save Null to database but empty string. so you can remove null=False

the search url name is home which logically not really approriate. it should be changed to search or search_view and then you can refer to it via url tag:

action="{% url 'search' %}"

this is useful if someone should look over your code. "Readability counts" ;)

and finally, put this to your home.html (actually you must already have it)

{% for asset in desc %}
    <div>
        {{ asset.asset_desc }} <br>
        {{ asset.base_choice }} <br>
        {{ asset.asset_user }}
    </div>
{% endfor %}

I hope, this helps

like image 116
doniyor Avatar answered Aug 26 '26 20:08

doniyor


You have not provided the template or the HTML portion where you list the results. You should consider the name of you context variable, but by following your name, you should list the results like this:

{% for asset in desc %}
    <div>
    {{ asset }}
    </div>
{% endfor %}

Anything else looks correct.

Hope it helps

like image 40
andresgz Avatar answered Aug 26 '26 22:08

andresgz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!