Duplicate entry in database on refreshing the submitted form

106 views
Skip to first unread message

Satinderpal Singh

unread,
Sep 29, 2012, 6:33:49 AM9/29/12
to django...@googlegroups.com
I am using mysql database with my project, and made a user input form
with submit button. After submitting the form, it produces an html
output, If i again reload the output page it fills a duplicate entry
in the database. And on every refresh, it continues to add entries in
the database.
How will i prevent these duplicate entries on refreshing.

Here is the views of my code related to that form:
def header(request):
if request.method=='POST':
form = headForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
form.save()
zee = head.objects.aggregate(Max('id'))
mee =zee['id__max']
Head = head.objects.filter(id = mee)
return render_to_response('report/header.html',
{'Head':Head,},context_instance=RequestContext(request))

else:
form = headForm()
return render_to_response('report/report.html', {"form":form},
context_instance=RequestContext(request))

And the models are described as:
class head(models.Model):
refrence_no = models.CharField(max_length=255) #college
reference letter no.
dispatch_report_date = models.CharField(max_length=255) #report
dispatch date, to the client
date_of_testing = models.CharField(max_length=255) #date on
which test is performed
subject = models.CharField(max_length=255)
reference = models.CharField(max_length=255) #client reference letter no.
column_1 = models.CharField(max_length=255,blank=True)
column_2 = models.CharField(max_length=255,blank=True)
column_3 = models.CharField(max_length=255,blank=True)

def __str__(self):
return self.subject

class headForm(ModelForm):
class Meta :
model = head

Thanks in advance.

--
Satinderpal Singh
http://satindergoraya.blogspot.in/
http://satindergoraya91.blogspot.in/

Timster

unread,
Sep 29, 2012, 7:52:49 AM9/29/12
to django...@googlegroups.com
After saving the form, instead of doing a render_to_response, you should redirect the user to another page.

Satinderpal Singh

unread,
Sep 29, 2012, 8:13:52 AM9/29/12
to django...@googlegroups.com
On Sat, Sep 29, 2012 at 5:22 PM, Timster <timsha...@gmail.com> wrote:
> After saving the form, instead of doing a render_to_response, you should
> redirect the user to another page.
Thanks Timster, it works!

Amyth Arora

unread,
Sep 29, 2012, 2:59:51 PM9/29/12
to django...@googlegroups.com
As Tim said use "redirect" instead of "render". Alternatively on form submit you can also have some kind of validation for ex: Query for one reference_no can take place only once in 24 hours and if it is being submitted twice return an error message. This way it will be more user friendly. Also, you can add a "unique" field in your models to achieve this.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Thanks & Regards
----------------------------

Amyth [Admin - Techstricks]
Email - aroras....@gmail.com, ad...@techstricks.com
Twitter - @mytharora
http://techstricks.com/
Reply all
Reply to author
Forward
0 new messages