Как получить текст из пользовательского ввода и затем установить его в условие в html шаблоне?
Я делаю лабораторную работу, создавая веб-представление для доступа к данным из mysql, поэтому у меня есть некоторые конфигурации ниже:
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),
]
Я уже подключился к базе данных студентов в mysql, и могу показать все или отфильтровать, что я хочу с помощью кода, теперь я хочу иметь поле ввода, чтобы пользователь мог ввести свое имя пользователя, затем, когда они нажмут на кнопку отправить, он покажет только их информацию. Не могли бы вы помочь в моем случае?
Вы можете получить текст ввода с помощью формы. Затем передайте этот текст шаблону в качестве контекста.
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>