Rego Rego - 3 months ago 51
Python Question

django clear form field after a submit

I have an upload form,After every form submit,i want to clear the posted data,actually the form is holding the submitted data.I know that, this problem can be solved if i redirect my page to some other page,but i don't want to redirect my page,because after submitting data,an success message will show in that page.so how can i clear my form without redirecting my page?

this is my views.py file

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))


and this is my forms.py file

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',}))

Answer

I have solved it.In the views.py ,After saving form just assign the empty form , like that

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))

no need to Redirect Your page.

Comments