本文介绍了在 Django 中提交后如何清除表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个:
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?
推荐答案
表单提交后重定向是标准的,以防止重复.
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('')
这篇关于在 Django 中提交后如何清除表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!