Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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',}))
like image 291
Md. Tanvir Raihan Avatar asked Feb 12 '23 04:02

Md. Tanvir Raihan


2 Answers

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.

like image 152
Md. Tanvir Raihan Avatar answered Feb 15 '23 11:02

Md. Tanvir Raihan


While Rego's answer is technically correct, Django best practices dictate that you do a HttpResponseRedirect after a form submission. This reduces the likelihood of accidental multiple submissions.

You have two options for sending data to the redirected page: you could either redirect to a page that indicates the form was submitted successfully, and/or you can use session variables.

http://docs.djangoproject.com/en/stable/topics/http/sessions/

Since you can't send your view's local variables to a view that you redirect to, you can save that data in session variables that will be available to any subsequent sessions until it expires or is deleted.

For instance, you could put the user's name in a session variable in one view:

# view.py (the one with the form)

request.session['name'] = form.cleaned_data['name']

And then in the view that processes your success notice:

# view.py (form submission successful)

string = "Hi there, " + request.session['name'] + "!  How are you today?"

This is preferable to not doing a redirect as strongly suggested by the Django gods.

like image 42
pyrodney Avatar answered Feb 15 '23 10:02

pyrodney