Home > Software engineering >  how to get the text from user input then set it in to the condition in html template?
how to get the text from user input then set it in to the condition in html template?

Time:04-26

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)

def search(request):
    data = request.POST.get('name')
    print(data)
    return render(request, "search.html")

student\templates\search.html

   <h1>input your name then press submit button</h1>
    <form method="post">
        {% csrf_token %}
        name: <input type="text" name="name">
        <button type="submit">Submit</button>
    </form>

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 >
<table >
    <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),
]

how to get "data" taken from input box when user submit in search.html template , then assign it into {% if stud.fname == data %} in show.html template ?

CodePudding user response:

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 >
<h1> User Input </h1>
<form action="" method="post">
    {% csrf_token %}
    <input type="text" name="my_input">
    <button type="submit">Submit</button>
</form> 


<table >
    <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>
  • Related