如何在Django中提交后清除表单字段

时间:2022-04-26 20:34:02

I've this:

我是这样的:

def profile(request, username):
if request.method == 'POST':
    if request.user.is_authenticated():
        new_message = Message(author = request.user)
        form = MessagesForm(request.POST, instance = new_message)
    else:
        form = MessagesForm(request.POST)
    if form.is_valid():
        form.save()
else:
    to_user = User.objects.get(username = username)
    form = MessagesForm(initial = {'user': to_user.pk})


return render(request, "profile.html", {'username': username, 'form': form, 'messages': messages})

This form submit a message and return the same page. My problem is that after the submit I see again my field filled with my information. How to clear it after the submit?

此表单提交消息并返回相同的页面。我的问题是,提交后我再次看到我的字段充满了我的信息。提交后如何清除?

3 个解决方案

#1


20  

It's standard to redirect after form submission to prevent duplicates.

在表单提交后重定向是标准的,以防止重复。

Just return a redirect to your form on success.

只需在成功时将重定向返回到表单。

if form.is_valid():
    form.save()
    return http.HttpResponseRedirect('')

#2


15  

After saving form instead of showing post dict assign the empty form

保存表单而不是显示post dict分配空表单

form = EmployeeForm()
    if request.method == "POST":
        pDict = request.POST.copy() 
        form = EmployeeForm(pDict) #if not valid shows error with previous post values in corresponding field
        if form.is_valid():
            form.save()
            form = EmployeeForm() # show empty form no need to give HttpResponseRedirect()

#3


0  

Usually you can initialize the same empty form after you have saved datas:

通常,您可以在保存数据后初始化相同的空表单:

if request.method == "POST":
    rf = RegistrationForm(request.POST)
    if rf.is_valid():
        print 'Saving datas..'
        #logic to save datas
        rf = PreRegistrationForm()
        return render_to_response('registration/confirmation_required.html', {'settings': settings}, context_instance=RequestContext(request))

#1


20  

It's standard to redirect after form submission to prevent duplicates.

在表单提交后重定向是标准的,以防止重复。

Just return a redirect to your form on success.

只需在成功时将重定向返回到表单。

if form.is_valid():
    form.save()
    return http.HttpResponseRedirect('')

#2


15  

After saving form instead of showing post dict assign the empty form

保存表单而不是显示post dict分配空表单

form = EmployeeForm()
    if request.method == "POST":
        pDict = request.POST.copy() 
        form = EmployeeForm(pDict) #if not valid shows error with previous post values in corresponding field
        if form.is_valid():
            form.save()
            form = EmployeeForm() # show empty form no need to give HttpResponseRedirect()

#3


0  

Usually you can initialize the same empty form after you have saved datas:

通常,您可以在保存数据后初始化相同的空表单:

if request.method == "POST":
    rf = RegistrationForm(request.POST)
    if rf.is_valid():
        print 'Saving datas..'
        #logic to save datas
        rf = PreRegistrationForm()
        return render_to_response('registration/confirmation_required.html', {'settings': settings}, context_instance=RequestContext(request))