Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#18658 closed Cleanup/optimization (fixed)

Better support for exceptions / error messages in custom admin actions

Reported by: Danilo Bargen Owned by: thiderman
Component: contrib.admin Version: 1.4
Severity: Normal Keywords: admin, exception handling, ui/ux, docs
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The docs don't mention any way to create error messages in custom admin actions. According to an answer on Stackoverflow, this can be achieved with:

from django.contrib import messages

# Then, when you need to error the user:
messages.error(request, "The message")

There are three issues with this:

  • There's a message_user method on a ModelAdmin, but no similar thing for errors. This seems inconsistent.
  • It's not mentioned in the docs. Errors in actions need to be handled.
  • The error message isn't rendered with red background color, as it should be. A possible fix is described here.

Change History (9)

comment:1 by Travis Swicegood, 12 years ago

Triage Stage: UnreviewedAccepted

Thanks. Definitely something we should add. Probably something along the lines of adding a level kwarg to message_user.

comment:2 by Travis Swicegood, 12 years ago

Type: UncategorizedCleanup/optimization

comment:3 by thiderman, 12 years ago

Owner: changed from nobody to thiderman

I'll tackle this one as part of my work at the Django sprint in Stockholm!

comment:4 by thiderman, 12 years ago

Has patch: set

comment:5 by Jannis Leidel, 12 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Preston Holmes, 12 years ago

Allowing a string for the level for convenience seems fine - but we should still accept messages.INFO style constants.

I've made that change and added some more docs and release note here:

https://github.com/ptone/django/compare/72bac6e8e12bd28cedb6874810812fcd3d137133…4b4f7c5dbf795b3db379e2100936bba06c71c718

Thanks for your work on this

comment:7 by thiderman, 12 years ago

Looks good. Thanks for the improvements as well!

comment:8 by Preston Holmes <preston@…>, 12 years ago

Resolution: fixed
Status: newclosed

In edf7ad36faab8d45aafe1f96feaf46794de22fc1:

Fixed #18658 -- Improved ModelAdmin.message_user API

Thanks to Lowe Thiderman for the patch and tests

comment:9 by Preston Holmes <preston@…>, 12 years ago

In 3a0e0230f76131cf23c0fdd0b18f3dd718c0f62f:

[1.5.x] Fixed #18658 -- Improved ModelAdmin.message_user API

Thanks to Lowe Thiderman for the patch and tests

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