How to get the text from user input then set it in to the condition in html template?

I'm doing a lab creating the web view to access the data from mysql, so I have some config below:

student\views.py

from django.shortcuts import render, redirect
from .models import Student

# Create your views here.
def show(request):
    students = Student.objects.all()
    student_dict = {'student':students}
    return render(request, "show.html",student_dict)

student\templates\show.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Django CRUD Operations</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> 
</head>
<body>
<div class="container">
<table class="table table-striped">
    <thead>
      <tr>
        <th>Student ID</th>
        <th>Roll</th>
        <th>Class</th>
        <th>First Name</th>
        <th>Last Name</th>
      </tr>
    </thead>
    <tbody>
    {% for stud in student %}  
      <tr>
        {% if stud.fname == 'abc' %}   # I would like to make an input box for user can input here
        <td>{{stud.id}}</td>
        <td>{{stud.roll}}</td>
        <td>{{stud.sclass}}</td>
        <td>{{stud.fname}}</td>
        <td>{{stud.lname}}</td>
        {% endif %}
      </tr>
      {% endfor %} 
    </tbody>
</table>    
</div>
</body>
</html>

student\urls.py

from django.urls import path
 
from . import views 
 
urlpatterns = [ 
     path('show/', views.show),
]

I have connectted to database student in mysql already , and could show all or filter which i want by code , now i want to have an input box for user can input their username , then when they click on submit it will show only their info. Could you please assist for my case ?

You can get the input text with a form. Then pass this text to template as context.

student/views.py

from django.shortcuts import render, redirect
from .models import Student
# Create your views here.
def show(request):
    input_text = request.POST.get('my_input', None)
    students = Student.objects.all()
    context = {
        'student':students,
        'input_text': input_text
    }
    return render(request, "show.html", context)

student/templates/show.html

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Django CRUD Operations</title>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> 
</head>
<body>
<div class="container">
<h1> User Input </h1>
<form action="" method="post">
    {% csrf_token %}
    <input type="text" name="my_input">
    <button type="submit">Submit</button>
</form> 


<table class="table table-striped">
    <thead>
      <tr>
        <th>Student ID</th>
        <th>Roll</th>
        <th>Class</th>
        <th>First Name</th>
        <th>Last Name</th>
      </tr>
    </thead>
    <tbody>
    {% for stud in student %}  
      <tr>
        {% if stud.fname == input_text %}  
        <td>{{stud.id}}</td>
        <td>{{stud.roll}}</td>
        <td>{{stud.sclass}}</td>
        <td>{{stud.fname}}</td>
        <td>{{stud.lname}}</td>
        {% endif %}
      </tr>
      {% endfor %} 
    </tbody>
</table>    
</div>
</body>
</html>
Back to Top