#21412 closed Bug (fixed)

Django message framework called with None

Reported by: merb Owned by: syphar
Component: contrib.messages Version: 1.6
Severity: Normal Keywords: messages
Cc: c.schmitt@… Triage Stage: Ready for checkin
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Currently, this is a very small bug and i don't now the first occurance, since i first used the message framework at the last evening.

I used Django 1.6 and installed everything. I put the Message Framework into the middleware, the context preprocessor and the INSTALLED APPS.
And then I did something which happend, cause i was very sleepy.

I made a generic.FormView like this:

class CreateUserView(generic.FormView):
    template_name = 'envisia_backup/create_user.html'
    form_class = UserForm

    def get_success_url(self):
        return reverse('envisia:index')

    def form_valid(self, form):
        data = form.cleaned_data
        user = User.objects.create(**data)
        user.is_staff = True
        user.is_superuser = True
        user.save()
        messages.success(request, 'User erfolgreich erstellt!')
        return HttpResponseRedirect(self.get_success_url())

Since I was sleepy I really didn't know where the mistake was.
The error message always was that the message framework wasn't installed and I should put it in the middleware and in the INSTALLED_APPS.

Why did this happen? Currently it happend cause I called messages.success with None.
request wasn't defined. so it was None. It took a while since I found the error since I never got a correct error message.

Currently the fix would be really simple. But I don't want to make a patch until know since I don't know if it is wished that the messages framework should make a assert if we call it with a Request object or not.
If it is wished that the message framework could only be called with a request object, than i would make a patch with a corresponding test. But only if this is really wished.

Change History (7)

comment:1 Changed 21 months ago by bmispelon

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Hi,

The code you showed would actually raise a NameError (it's the error python raises when you use a variable without defining it first).

The code is wrong but the behavior you describe is actually correct: passing anything but a request object (actually, anything without a _messages attribute) to add_message raises an error telling you to install the message middleware.

I can see this being confusing, so if you have ideas on how to improve the situation, please do share them.

Thanks.

comment:2 Changed 21 months ago by merb

Thats correct, normally it would raise NameError, but it didn't raised the NameError so it always raised that the middleware was not installed.
I was very sleepy thats why i found out, that something is really wrong.

I would make assertion checks on

messages.add_message
messages.debug
messages.info
messages.success
messages.warning
messages.error

but that would raise another problem, what happens if somebody makes a custom Request that inerhits from the actual request object, would this work with a assertion check, too?

comment:3 Changed 21 months ago by claudep

If this didn't raise NameError, it is probably because request was defined somewhere in your file. If we can find a rather common possible use case where request is None, then it might be worth asserting request is not None in add_message.

comment:4 Changed 21 months ago by syphar

will work on a patch for this

comment:5 Changed 21 months ago by syphar

  • Owner changed from nobody to syphar
  • Status changed from new to assigned

comment:6 Changed 21 months ago by bmispelon

  • Triage Stage changed from Accepted to Ready for checkin

Looks good. I'll make sure the test suite passes and commit it.

comment:7 Changed 21 months ago by bmispelon

  • Resolution set to fixed
  • Status changed from assigned to closed
Note: See TracTickets for help on using tickets.
Back to Top