Opened 11 years ago

Closed 11 years ago

#21054 closed Bug (needsinfo)

SimpleLazyObject for request.user not correctly evaluated inside RequestContext

Reported by: Giovanni Allegri 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

Description

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):
        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 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

Giovanni

Attachments (1)

djbug-21054.zip (6.0 KB ) - added by polmuz 11 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 by DanRJohnson, 11 years ago

Owner: changed from nobody to DanRJohnson
Status: newassigned

comment:2 by DanRJohnson, 11 years ago

Owner: DanRJohnson removed
Status: assignednew

comment:3 by polmuz, 11 years ago

Owner: set to polmuz
Status: newassigned

comment:4 by polmuz, 11 years ago

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

by polmuz, 11 years ago

Attachment: djbug-21054.zip added
Note: See TracTickets for help on using tickets.
Back to Top