如何在Django视图中获得输入文本字段的id ?



目前在我的表单中有:

<input type="text" name="textAnswer" maxlength="50" id="ed57a954-7afd-47ac-b6d4-979198455aa5" class="textinput textInput form-control">

当表单提交时,我想要视图中这个输入的id。我知道如何在视图中获取用户在textInput中键入的值,但我不知道如何提取该输入的id。

目前我正在提取的值与此代码:

userTypedValue = request.POST.get('textAnswer')

也许你可以添加新的字段来存储id。

#forms.py
from django import forms
class SomeForms(forms.Form):
id = forms.CharField(
widget=forms.TextInput(
attrs={               
"class": "form-control"
}
))
value = forms.CharField(
widget=forms.TextInput(
attrs={               
"class": "form-control"
}
))

然后,考虑使用form data

# views.py
@login_required(login_url="/login/")
def someform_view(request):
if request.method == 'POST':
form = SomeForms(request.POST)
....

记住传递from到view

front.html
<input type="hidden" value="ed57a954-7afd-47ac-b6d4-979198455aa5">

最新更新