hi group
I have a situation here. I need to make sure that a field is unique
in the database. In my model i have the following:
url= models.CharField (max_length=254, blank=False,
db_index=True,unique=True)
When i go to save, it throws the IntegrityError just like it should.
How can i trap that error and send it back to the form gracefully with
the data still intact and throw a form validation error message? I'm
assuming that it has to be done through clean data but i'm not sure
how to implement...
here is my view:
def Addme(request):
if request.method=='POST':
form=FrmWebPage(request.POST)
if form.is_valid(): #process valid form here
#clean form data and write to the database
if request.user.id:
createdby=request.user.id
else:
createdby=0
newpage = WebPage (
url_slug= manage_cleantext(request.POST.get
('url_slug','')),
active = int(manage_cleantext(request.POST.get
('active',''))),
on_rss = int(manage_cleantext(request.POST.get
('on_rss',''))) )
newpage.save() #save to WebContact table
return HttpResponseRedirect ('/manage/content/web-pages/
list/1/')
else: #form is not valid so return form with error messages
return render_to_response ('web_pages/addwebpage.html',
{'form':form},context_instance=RequestContext(request))
else: # form has not yet been posted so do so now
form=FrmWebPage() # establish a new instance of an empty form
return render_to_response ('web_pages/addwebpage.html',
{'form':form},context_instance=RequestContext(request))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---