我有一个上传表单,每次提交表单后,我想清除发布的数据,实际上表单保存提交的数据。我知道,如果我将我的页面重定向到其他页面,这个问题就可以解决,但我不想重定向我的页面,因为提交数据后,该页面会显示一条成功消息。那么如何在不重定向我的页面的情况下清除我的表单?
这是我的 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))
无需重定向您的页面。
我是一名优秀的程序员,十分优秀!