Opened 5 years ago

Closed 5 years ago

#21054 closed Bug (needsinfo)

SimpleLazyObject for request.user not correctly evaluated inside RequestContext

Reported by: giohappy Owned by: polmuz
Component: Template system Version: 1.5
Severity: Normal Keywords: template, form view
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I'm facing a wired behaviour in a FormView:

  • In my "form_valid" method I need to manage exceptions.
  • 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.
  • The error template contains the "user" variable.

I'm doing sometihng like this (I past a simplified version)

class ProjectCreateView(FormView):

    def form_valid(self, form):
        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 crashe seems to be caused when the render process tries to access 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


Attachments (1) (6.0 KB) - added by polmuz 5 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 5 years ago by DanRJohnson

Owner: changed from nobody to DanRJohnson
Status: newassigned

comment:2 Changed 5 years ago by DanRJohnson

Owner: DanRJohnson deleted
Status: assignednew

comment:3 Changed 5 years ago by polmuz

Owner: set to polmuz
Status: newassigned

comment:4 Changed 5 years ago by polmuz

Resolution: needsinfo
Status: assignedclosed
Type: UncategorizedBug

I can't reproduce this behavior, could you please provide some instructions on how to reproduce it? I'm attaching the project I used to test this, you can find the code in myapp.views

Changed 5 years ago by polmuz

Attachment: added
Note: See TracTickets for help on using tickets.
Back to Top