Code

Opened 21 months ago

Closed 17 months ago

Last modified 17 months ago

#18658 closed Cleanup/optimization (fixed)

Better support for exceptions / error messages in custom admin actions

Reported by: dbrgn 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.

Attachments (0)

Change History (9)

comment:1 Changed 20 months ago by tswicegood

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

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

comment:2 Changed 20 months ago by tswicegood

  • Type changed from Uncategorized to Cleanup/optimization

comment:3 Changed 17 months ago by thiderman

  • Owner changed from nobody to thiderman

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

comment:4 Changed 17 months ago by thiderman

  • Has patch set

comment:5 Changed 17 months ago by jezdez

  • Triage Stage changed from Accepted to Ready for checkin

comment:6 Changed 17 months ago by ptone

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 Changed 17 months ago by thiderman

Looks good. Thanks for the improvements as well!

comment:8 Changed 17 months ago by Preston Holmes <preston@…>

  • Resolution set to fixed
  • Status changed from new to closed

In edf7ad36faab8d45aafe1f96feaf46794de22fc1:

Fixed #18658 -- Improved ModelAdmin.message_user API

Thanks to Lowe Thiderman for the patch and tests

comment:9 Changed 17 months ago by Preston Holmes <preston@…>

In 3a0e0230f76131cf23c0fdd0b18f3dd718c0f62f:

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

Thanks to Lowe Thiderman for the patch and tests

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.