[英]How to get value in variable from Django template in views.py?
I am working in django and want to pass the value in views.py my code is我在 django 中工作,想在 views.py 中传递值我的代码是
template模板
{% for doctor in doctor_list %}
{% if citysearch == doctor.city %}
<h1>Name of doctor is </h1>
<form class="form" method="POST">
{% csrf_token %}
<input type="submit", class="btn view", name="{{doctor.contactNumber}}" value="View Profile">
</form>
{% endif %}
{% endfor %}
view看法
if request.method == 'POST':
selectdocnum = request.POST.get["doctor.contactNumber"]
print(selectdocnum)
return redirect('patientPannel')
This is not returning the value of doctor.contactNumber, and error is method object is not subscriptable这不是返回doctor.contactNumber的值,错误是方法对象不可下标
doctor.contactNumber
is a value , not a key that you should be using to lookup a value or as a name for a input field. doctor.contactNumber
是一个值,而不是您应该用来查找值或作为输入字段名称的键。 give the input field a better name and use that instead.给输入字段一个更好的名称并使用它。 You're also using the wrong input type, either use
hidden
if you don't expect it to be edited, or something like text
if it can be.您还使用了错误的输入类型,如果您不希望对其进行编辑,请使用
hidden
,或者如果可以的话,请使用text
东西。
<input type="hidden" name="contactNumber" value="{{doctor.contactNumber}}">
request.POST.get("contactNumber")
this is wrong,这是错误的,
<input type="submit", class="btn view", name="{{doctor.contactNumber}}" value="View Profile">
i think you have to do this我认为你必须这样做
{% for doctor in doctor_list %}
{% if citysearch == doctor.city %}
<h1>Name of doctor is </h1>
<form class="form" method="POST">
{% csrf_token %}
<input type="text" value="{{doctor.contactNumber}}" name="doctorcontactnumber">
<input type="submit" class="btn view" value="View Profile">
</form>
{% endif %}
{% endfor %}
your views你的意见
if request.method == 'POST':
selectdocnum = request.POST.get["doctorcontactnumber"]
print(selectdocnum)
return redirect('patientPannel')
It's normal, request.POST.get
is a method.很正常,
request.POST.get
是一个方法。 Use parenthesis.The right call is:使用括号。正确的调用是:
selectdocnum = request.POST.get("doctorcontactnumber")
For your input, you have jumbled name
and value
.对于您的输入,您混淆了
name
和value
。 Do this instead:改为这样做:
<input type="hidden" name="doctorcontactnumber" value="{{doctor.contactNumber}}">
<input type="submit", class="btn view", name="number" value="{{doctor.contactNumber}}">
if request.method == 'POST':
selectdocnum = request.POST.get("number")
print(selectdocnum)
return redirect('patientPannel')
in place of value you have to use the {{doctor.contactNumber}} and you have to get the data using the name(request.POST.get("number")) then it will get the data代替值,您必须使用 {{doctor.contactNumber}} 并且您必须使用 name(request.POST.get("number")) 获取数据,然后它将获取数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.