1

I have created django form.py file :

from django import forms
from models import ResumeModel


class ResumeForm(forms.ModelForm):
    username = forms.CharField(max_length = 200)
    first_name = forms.CharField(max_length = 200)
    last_name = forms.CharField(max_length = 200)
    fathers_name = forms.CharField(max_length = 200)
    email = forms.EmailField(required=True, label='Your e-mail address')
    message = forms.CharField(widget=forms.Textarea)
    class Meta():
        model = ResumeModel
        fields =('username','first_name','last_name','fathers_name','email','message')

views.py :

def save(request):
    if 'submit' in request.POST:
        form = ResumeForm(request.POST)
        if form.is_valid():
            form.save()
            form = ResumeForm()
            return render(request, '/success/', {'form': form})
    else:
        form = ResumeForm()
    args = {}
    args.update(csrf(request))
    args['form'] =form
    return render_to_response('create.html',args)

urls.py:

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^save/', 'Resume.views.save'),
    url(r'^success/$', TemplateView.as_view(template_name='success.html')),
)

Now all is working fine. problem is that as i submit form it moves to success page and when i go back to previous page that is /save/ page it holds all the values in fields that i submitted. How to clear the fields if i press back button to go back to that form page having url as "/save/ "

Tameen Malik
  • 1,258
  • 6
  • 17
  • 45

2 Answers2

1

This is about the browsers behaviour. Browser shows u the page in its cache instead of calling a new page.

U may workaround this by using javascript.Unfortunately browser also dont run the javascript codes again.

But u may try :

$(window).bind("pageshow", function() {
  // Clean form values
});

by JQuery

obayhan
  • 1,636
  • 18
  • 35
1

This is a browser implementation detail. You might be able to hack your way around it with JavaScript or break the cache etc. The subject is discussed in this stackoverflow question.

However a nicer UI approach might be to use AJAX to POST a serialised version of the form. If you successfully validate and process that form in your view (inserting a new row into your resume model table etc), you could then send a JSON response which might invoke some JavaScript to clear the form fields (maybe using .reset()). The user is then free to submit another form easily if that is the requirement.

Also note it is recommended that you use a HttpResponseRedirect after a successful POST (you have a render() response at the moment). This stops the users re-submitting the form again and potentially duplicating rows in your databases etc.

Community
  • 1
  • 1
dannymilsom
  • 2,386
  • 1
  • 18
  • 19