0

So i want to take the user input and compare it to data present in the sqlite3 db, and if matches I'd like to print that whole row, using django orm.

models.py

from django.db import models


class Inventory(models.Model):
    item_bc = models.CharField(max_length=100)
    item_details = models.CharField(max_length=100)

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('search', views.search, name='search'),
]

views.py


    from django.shortcuts import render
from django.http import HttpResponse
from models import Inventory

# Create your views here.


def index(request):
    return render(request, 'form.html')


def search(request):
    search_input = request.POST.get('barcode')
    data = Inventory.objects.filter(item_bc=search_input).values()
    return render(request, 'result.html', {"data": data})

I really appreciate your time and help, thank you!

i think adding logic to the search function to compare should work but extremely new to django and dont really know on how to start..

1

1 Answer 1

0

html

<form action="search" method="post">
    BARCODE: <input type="text" name="barcode">
    <br>
    <br>
    <input type="submit">
</form>

in view.py you can fetch searched input like this and filter.

def search(request):
    search_input = request.POST['barcode']
    data = ModelName.objects.filter(fieldname__icontains=search_input)
    return render(request, 'result.html', {"data":data})
Sign up to request clarification or add additional context in comments.

10 Comments

Hey there, so i tried the solution and now the problem is when i try to run the whole thing i get different error all associated with the line "data = modelname.objects.filter thing i think my database is not getting connected or something like that, if u can please help
can you share error ?and the code you tried also
this is the code you gave and i edited it slightly
and this is giving me Attribute error saying nonetype object has no attribute filter..
update the question with Fresh code and Models.
|

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.