Opened 4 years ago

Closed 4 years ago

#16373 closed Cleanup/optimization (invalid)

Django admin templates trigger a DeprecationWarning

Reported by: slinkp Owned by: nobody
Component: contrib.admin Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

During test runs of my app with Django 1.3 installed, I get this:

"DeprecationWarning: The user messaging API is deprecated. Please update your code to use the new messages framework."

I've traced this to a page which uses a template which extends admin/base.html.
I'm guessing it comes from these lines:

        {% if messages %}
        <ul class="messagelist">{% for message in messages %}
          <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>

It's mildly annoying that I get the warning even though AFAICT my code doesn't use that API at all - the warning sent me on a wild goose chase.

I'm assuming we can't just remove or update that part of base.html without breaking third-party code that still uses the deprecated API. Is there any way we could trigger the warning only when you actually *use* the deprecated API?

Change History (4)

comment:1 Changed 4 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

The warning is only issued when the old API is used. One of the times the old API is used is when the new message framework is not enabled (see https://docs.djangoproject.com/en/1.3/ref/contrib/messages/#enabling-messages) and yet there is an application (in this case admin) that uses messages, falling back to the old API in the absence of the new framework. It's unfortunate perhaps that the warning is not clearer that "update your code" may mean "include proper settings for messages in settings.py", but it is kind of too late to fix that now since the old messages code is entirely removed in 1.4.

comment:2 Changed 4 years ago by slinkp

Thanks. Adding the right context processor & middleware as per the docs solved it. It turns out that the old API is used by 'django.contrib.auth.context_processors.auth' which was in my TEMPLATE_CONTEXT_PROCESSORS.

comment:3 follow-up: Changed 4 years ago by anonymous

  • Easy pickings set
  • Resolution invalid deleted
  • Status changed from closed to reopened

The documentation on the aforementioned documentation page states:

"If you don’t want to use messages, you can remove the MessageMiddleware line from MIDDLEWARE_CLASSES, the messages context processor from TEMPLATE_CONTEXT_PROCESSORS and 'django.contrib.messages' from your INSTALLED_APPS."

However, if MessageMiddleware is removed from the settings file then you get "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/models.py:393: DeprecationWarning: The user messaging API is deprecated. Please update your code to use the new messages framework. category=DeprecationWarning)" message. The documentation needs to be amended to only say:

"If you don’t want to use messages, you can remove the messages context processor from TEMPLATE_CONTEXT_PROCESSORS and 'django.contrib.messages' from your INSTALLED_APPS."

comment:4 in reply to: ↑ 3 Changed 4 years ago by carljm

  • Resolution set to invalid
  • Status changed from reopened to closed

Replying to anonymous:

The documentation on the aforementioned documentation page states:

"If you don’t want to use messages, you can remove the MessageMiddleware line from MIDDLEWARE_CLASSES, the messages context processor from TEMPLATE_CONTEXT_PROCESSORS and 'django.contrib.messages' from your INSTALLED_APPS."

However, if MessageMiddleware is removed from the settings file then you get "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/models.py:393: DeprecationWarning: The user messaging API is deprecated. Please update your code to use the new messages framework. category=DeprecationWarning)" message. The documentation needs to be amended to only say:

"If you don’t want to use messages, you can remove the messages context processor from TEMPLATE_CONTEXT_PROCESSORS and 'django.contrib.messages' from your INSTALLED_APPS."

This amendment would only be correct if one assumes that contrib.admin is a required part of Django. It is not, therefore the current sentence listing all three pieces of contrib.messages as remove-able is correct. The admin docs already make clear its dependency on contrib.messages and the messages middleware.

If there are other related bits of documentation that could be improved here (for instance, mentioning the admin dependency in this part of the messages docs, which I could see as useful), please open that as a separate ticket for clarity.

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