How to store user input in a variable in django python
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.
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> Assignment 3</title>
</head>
<body>
<h1> Please enter the product barcode in the text box below: </h1>
<form action="search">
BARCODE: <input type="text" name="barcode">
<br>
<br>
<input type="submit">
</form>
</body>
</html>
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
# Create your views here.
def index(request):
return render(request, 'form.html')
def search(request):
return render(request, 'result.html')
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..
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})