【问题标题】:how to get id of the input text field in Django Views? [duplicate]如何在 Django Views 中获取输入文本字段的 id? [复制]
【发布时间】:2021-11-11 02:21:15
【问题描述】:

目前在我的表单中我有这个:

<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')

【问题讨论】:

标签: python html django django-views django-forms


【解决方案1】:

也许您可以添加新字段来存储 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"
            }
        ))

那么,为了使用表单数据

# 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">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 2014-02-25
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    相关资源
    最近更新 更多