gpt4 book ai didi

python - 提交后django清除表单字段

转载 作者:太空宇宙 更新时间:2023-11-03 14:22:38 26 4
gpt4 key购买 nike

我有一个上传表单,每次提交表单后,我想清除发布的数据,实际上表单保存提交的数据。我知道,如果我将我的页面重定向到其他页面,这个问题就可以解决,但我不想重定向我的页面,因为提交数据后,该页面会显示一条成功消息。那么如何在不重定向我的页面的情况下清除我的表单?

这是我的 views.py 文件

def UserImageUpload(request):


if request.method == 'POST':
form = DocumentForm(request.POST,request.FILES)
if form.is_valid():
messages.add_message(request, messages.SUCCESS, 'Your Image upload is waiting for Admin approval')


newdoc = Photo(photo = request.FILES['photo'],watermarked_image=request.FILES['photo'],user = request.user,name = request.POST['name'],description = request.POST['description'],keyword = request.POST['Image_Keyword'],uploaded_time=datetime.datetime.now(),Certified=request.POST['Certification'])

newdoc.save()
else:
messages.add_message(request, messages.ERROR, 'Please Complete All Fields To Submit Your Image')



else:
form = DocumentForm()

uploaded_image = Photo.objects.all()

return render_to_response('myprofile/user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request))

这是我的forms.py文件

from django import forms

class DocumentForm(forms.Form):
photo = forms.ImageField(
label='Select A file',)
name = forms.CharField(label='Image Name',max_length=50,widget=forms.TextInput(attrs={'class' : 'form-control',}))
Certification = forms.BooleanField(label='I certify that this is my original work and I am atlest 18 years of age')
description = forms.CharField(label='Image Description',max_length=500,widget=forms.TextInput(attrs={'class' : 'form-control',}))
Image_Keyword = forms.CharField(label='Keywords',widget=forms.TextInput(attrs={'class' : 'form-control',}))

最佳答案

我已经解决了。在 views.py 中,保存表单后只需分配空表单,就像那样

def UserImageUpload(request):

if request.method == 'POST':
form = DocumentForm(request.POST,request.FILES)
if form.is_valid():
messages.add_message(request, messages.SUCCESS, 'Your Image upload is waiting for Admin approval')
newdoc = Photo(photo = request.FILES['photo'],watermarked_image=request.FILES['photo'],user = request.user,name = request.POST['name'],description = request.POST['description'],keyword = request.POST['Image_Keyword'],uploaded_time=datetime.datetime.now(),Certified=request.POST['Certification'])
newdoc.save()
#Assign the empty form,it will empty the form after a successful form submission
form=DocumentForm()
else:
messages.add_message(request, messages.ERROR, 'Please Complete All Fields To Submit Your Image')
else:
form = DocumentForm()
uploaded_image = Photo.objects.all()
return render_to_response('myprofile/user_image_upload.html',{'uploaded_image':uploaded_image,'form':form},context_instance = RequestContext(request))

无需重定向您的页面。

关于python - 提交后django清除表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25470452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com