#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

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 19 months ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 19 months ago by DanRJohnson

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to DanRJohnson
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 19 months ago by DanRJohnson

  • Owner DanRJohnson deleted
  • Status changed from assigned to new

comment:3 Changed 19 months ago by polmuz

  • Owner set to polmuz
  • Status changed from new to assigned

comment:4 Changed 19 months ago by polmuz

  • Resolution set to needsinfo
  • Status changed from assigned to closed
  • Type changed from Uncategorized to Bug

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 19 months ago by polmuz

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