Hi German,
thanks for the reply. I apologize for the delay, but I didn't receive the 
notification :(
I've definetly improved the form workflow defining a validator inside my 
form. My validator check avoids the IntegrityError from Postgres.

Anyway, in case such an error happens, how should one manage it? How to 
reference the current transaction to roll it back?

giovanni

Il giorno sabato 7 settembre 2013 04:02:03 UTC+2, Germán Larraín ha scritto:
>
> What DB engine are you using? If it is PostgreSQL, you need to roll back 
> the transaction after a database error exception (e.g. IntegrityError).
>
> By the way, I think what you are doing is wrong. If the form data is 
> invalid, then you need to call ..... form_invalid
>
> On Friday, September 6, 2013 11:41:34 AM UTC-5, giovanni allegri wrote:
>>
>> I've opened a new thread, which relates to a previous 
>> one<https://groups.google.com/forum/#!topic/django-users/Ng0jocuFdqw>from me.
>> Digging into a wired behaviour in a FormView I've arrived to the 
>> following problem:
>>
>>  - In my "form_valid" method I need to manage exceptions situations.
>>  - In case of an exception I want to pass the response flow to an 
>> external method, in an other module
>>  - This external method returns a "render_to_response", in which I also 
>> pass a RequestContext instance built from the "FormView.request" object.
>>
>> Sometihng like this (I past a simplified version)
>>
>> class ProjectCreateView(FormView):
>>
>>     def form_valid(self, form):
>>         try:
>>             <something>
>>         except IntegrityError as e:
>>             return render_error(_('A project with the same title already 
>> exists'),self.request)
>>
>> def render_error(msg,request):
>>     #__dummy__ = request.user.is_authenticated()
>>     data = {'error':{'msg':msg}}
>>     return 
>> render_to_response('generic_error.html',data,context_instance=RequestContext(request))
>>
>> This causes a DatabaseError, because it crashes when it reaches the 
>> user.is_authenticated template variable. The crashed seems to be caused 
>> because of the SimpleLazyObject around the User instance. It seems it's not 
>> correctly setup, or whatelse... I don't know.
>>
>> This problem disappears if I use the __dummy__ variable in the previous 
>> snippet.
>> This call seems to make the SimpleLazyObject "prepared" for later calls 
>> (within the context processors).
>>
>> Why does this happen???
>> It never happens in other view and generic views I'm using in my project. 
>> What's wrong with the form view?
>>
>> Notice that 
>>
>> 1 - the same happens even if I set self.template_name and call 
>> self.render_to_response.
>> 2 - it doesn't happen during the normal form view workflow
>>
>> Giovanni
>>
>>
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to