简体   繁体   English

提交后如何只清除一个特定的表单字段?

[英]How can I clear only one specific form field after submit?

I'm creating a chat application and there are 2 fields, receiver's name and body, I would like to clear the body which is the comment field without clearing the receiver's name after form submit, how can I achieve that ? 我正在创建一个聊天应用程序,其中有两个字段,收件人的姓名和正文,我想清除表单正文,即comment字段,而不在提交表单后清除收件人的姓名,我该如何实现?

views.py views.py

if request.method == 'POST':
    form = userCommentForm(request.POST, request.FILES)
    if form.is_valid():
        form.save(client=request.user)
        > clear comment field.

forms.py ("worker" field is the receiver's name.) form.py (“工作人员”字段是接收方的名称。)

class userCommentForm(forms.Form):
    worker = CommaSeparatedUserField(label=_(u"worker"), widget=forms.TextInput(attrs={'readonly': 'readonly'}))
    comment = forms.CharField(label=_(u"comment"), widget=forms.Textarea())

    def save(self, client):
        workers = self.cleaned_data['worker']
        comment = self.cleaned_data['comment']
        message_list = []
        for worker in workers:
            msg = userComment(client=client, worker=worker, comment=comment,)

            if None is not None:
                msg.parent_msg = None
                None.save()
            msg.save()
            message_list.append(msg)

        return message_list
        form.save(client=request.user)

Any suggestions ? 有什么建议么 ?

You have some misunderstanding here. 您在这里有些误会。 After a form is submitted and processed in a web application, you shouldn't continue with anything for current request anymore, but you should send GET request to another web page. 在Web应用程序中提交并处理表单后,您不应再继续处理当前请求,而应将GET请求发送到另一个网页。 Thus, it's not possible to set a form field as empty after submission. 因此,提交后无法将表单字段设置为空。

What I suggest is that when your form is submitted, you redirect to the same page but with a GET parameter storing what name was submitted, then in the beginning of the view you feed the form with the name as initial parameter: 我的建议是,提交表单时,您将重定向到同一页面,但是使用GET参数存储提交的名称,然后在视图的开头输入名称作为初始参数的表单:

def view_method(request):
    default_receiver = request.GET.get('receiver', None)
    form = userCommentForm(request.POST or None,
                           request.FILES or None,
                           initial={'worker': default_receiver})
    if form.is_valid():
        form.save(client=request.user)
        current_receiver = form.cleaned_data['worker']
        response = redirect('current-page-url-name')
        response['Location'] += '?receiver=%s' % current_receiver
        return response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 提交后如何仅清除表单中的特定字段? - How to clear only a specific field from a form after submission? 提交后如何在表单中保留字段值? - How can I keep field values in a form after submit? 提交后django清除表单字段 - django clear form field after a submit 确认后如何提交 Django 表格 - How can I Submit Django Form after Confirmation flask-mongoengine model_form 缺少提交字段 - 我如何 append 一个到表单? - flask-mongoengine model_form is missing a submit field - how do I append one to the form? 如何清除特定的流式缓存? - how can I clear specific streamlit cache? 提交后如何使 Django 表单字段为空白? - How to make Django form field blank after submit? 在使用.clear()或.send_keys()后禁用按钮时,如何使用Python Selenium单击(绕过)提交按钮? - How can I click (bypass) a submit button using Python Selenium, when the button gets disabled after using .clear() or .send_keys()? 我的 django Web 应用程序保留了内存中的旧图像数据。 每次提交后如何清除它? - My django web app is holding on to old image data in memory. How can I clear it after every submit? Django表单:提交表单后选择字段 - Django form: select field after the submit of the form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM