1. In forms.py, create a form to be used for search
[python]
class ComputerSearchForm(forms.ModelForm):
class Meta:
model = Computer
fields = [‘computer_name’, ‘users_name’]
[/python]
2. Import ComputerSearchForm in views.py and update computer_list view to include a search form and POST if statement
[python]
def computer_list(request):
title = ‘List of all computers’
form = ComputerSearchForm(request.POST or None)
context = {
"title": title,
"form": form,
}
if request.method == ‘POST’:
queryset = Computer.objects.all().order_by(‘-timestamp’).filter(computer_name__icontains=form[‘computer_name’].value(),users_name__icontains=form[‘users_name’].value())
context = {
"title": title,
"queryset": queryset,
"form": form,
}
return render(request, "computers_list.html",context)
[/python]
3. Load crispy form tag {% load crispy_forms_tags %} in computer_list.html and add html form with {{form|crispy}} where you want the search form to appear
[python]
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<input class="btn btn-primary" type="submit" value="Search" />
</form>
[/python]
4. Update the model to allow blank and null from all the search fields.
[python]
blank=True, null=True
[/python]
I like it & how you tutor.
May I get the source code !!
All the codes are available in this site. Check it out here: http://www.arbcoms.com/category/computer-inventory-management-system/
Hey! Please I want to talk to you. If you can write to my email please
You can contact us here and we will get back to you the soonest. http://www.arbcoms.com/contact/
hi, arbadjie, excusme, when i try searching using the search fields, it does not show my search and is displaying this error, ValueError : Cannot use None as a query value, i make your sugestion post in the project stock management, but can’t solve this issue
is seems submit form from computer_list.html , doesn’t work,
im using inside, to simplify the css menu,
{% block content %}
{% endblock %}
imusing python 3.9 and django 3.1.5, if help
i solve rewriting from video 19 to video 20