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..

  • Does this answer your question? [Django and query string parameters](https://stackoverflow.com/questions/3711349/django-and-query-string-parameters). Better target: [How to get URL parameters in a Django view?](https://stackoverflow.com/questions/6225130/how-to-get-url-parameters-in-a-django-view) – Abdul Aziz Barkat Nov 23 '22 at 04:24

1 Answers1

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})
Hemal Patel
  • 878
  • 6
  • 16
  • 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 – Meet Patel Nov 23 '22 at 05:45
  • can you share error ?and the code you tried also – Hemal Patel Nov 23 '22 at 05:47
  • this is the code you gave and i edited it slightly – Meet Patel Nov 23 '22 at 05:56
  • and this is giving me Attribute error saying nonetype object has no attribute filter.. – Meet Patel Nov 23 '22 at 05:59
  • update the question with Fresh code and Models. – Hemal Patel Nov 23 '22 at 06:06
  • I have updated the code, also have added the models.py – Meet Patel Nov 23 '22 at 06:14
  • code should work. still getting an error? did you check you're getting input in this `search_input = request.POST.get('barcode')` try to print `search_input` – Hemal Patel Nov 23 '22 at 06:38
  • now everything else is fine but I am unable to import the inventory which is my modelclass in the view.py it is not allowing me to import showing error and not recognizing the file – Meet Patel Nov 23 '22 at 06:52
  • yea, that is working fine but the query set which filters the results needs to use my model class which i created in models.py now i am unable to import that model and when i do i get errors.. – Meet Patel Nov 23 '22 at 16:55
  • do this `from appName.models import Inventory`. – Hemal Patel Nov 24 '22 at 04:10