如何将值传递给带空格的输入字段值
How to pass values to input field's value with spaces
<form action="handleAppointment/" method="post" >
{% csrf_token %}
<div class="inputfield">
<label for="doctor" class="label">Doctor</label>
<input type="text" name="doctor" id="doctor" class="input" value={{ doctorName.name }} >
</div>
这是我的表格,我想要 database.but 的全部价值,这里 {{doctorName.name}} 显示的是 space.
之前的价值
def bookAppointment(request , id ):
doctor = Doctor.objects.filter(id = id ).first()
print(doctor.name)
context = {'doctorName': doctor}
return render(request , 'patient/appointmentForm.html' , context)
在 运行 此代码之后,它在终端中显示“Tapan Shah”作为输出。
这是全名,但它在前端的 space 值之前显示“Tapan”。
在 .py
文件中...
在 bookAppointment
函数中,您可以将一个额外的变量传递给模板。例如:
def bookAppointment(request, id ):
doctor = Doctor.objects.filter(id=id).first()
first_name = doctor.name.split()[0] # see here...
context = {'doctorName': doctor, 'first_name': first_name}
return render(request, 'patient/appointmentForm.html', context)
请注意,doctor.name.split()
将根据医生的姓名创建一个列表,例如 ['firstname'、'lastname']。通过使用 doctor.name.split()[0]
将调用医生的名字并将其分配给变量 first_name.
在 .html 文件中...
<div class="inputfield">
<label for="doctor" class="label">Doctor</label>
<input type="text" name="doctorName.name" id="doctorName.id" class="input" value={{ first_name }} >
</div>
因此,您可以使用 value={{ first_name }}
而不是 value={{ doctorName.name }}
,但请确保您拥有 id="doctorName.id"
,以便在需要时可以在其他地方参考此信息。
<form action="handleAppointment/" method="post" >
{% csrf_token %}
<div class="inputfield">
<label for="doctor" class="label">Doctor</label>
<input type="text" name="doctor" id="doctor" class="input" value={{ doctorName.name }} >
</div>
这是我的表格,我想要 database.but 的全部价值,这里 {{doctorName.name}} 显示的是 space.
之前的价值def bookAppointment(request , id ):
doctor = Doctor.objects.filter(id = id ).first()
print(doctor.name)
context = {'doctorName': doctor}
return render(request , 'patient/appointmentForm.html' , context)
在 运行 此代码之后,它在终端中显示“Tapan Shah”作为输出。 这是全名,但它在前端的 space 值之前显示“Tapan”。
在 .py
文件中...
在 bookAppointment
函数中,您可以将一个额外的变量传递给模板。例如:
def bookAppointment(request, id ):
doctor = Doctor.objects.filter(id=id).first()
first_name = doctor.name.split()[0] # see here...
context = {'doctorName': doctor, 'first_name': first_name}
return render(request, 'patient/appointmentForm.html', context)
请注意,doctor.name.split()
将根据医生的姓名创建一个列表,例如 ['firstname'、'lastname']。通过使用 doctor.name.split()[0]
将调用医生的名字并将其分配给变量 first_name.
在 .html 文件中...
<div class="inputfield">
<label for="doctor" class="label">Doctor</label>
<input type="text" name="doctorName.name" id="doctorName.id" class="input" value={{ first_name }} >
</div>
因此,您可以使用 value={{ first_name }}
而不是 value={{ doctorName.name }}
,但请确保您拥有 id="doctorName.id"
,以便在需要时可以在其他地方参考此信息。