Opened 3 months ago

Closed 3 months ago

#33051 closed Bug (fixed)

Model names with special characters don't highlight in admin site

Reported by: ꯸ ꬰ ꝛ⼻ↇ Owned by: Hasan Ramezani
Component: contrib.admin Version: 3.2
Severity: Normal Keywords: accents, unicode
Cc: Tom Carrick, Matthias Kestenholz Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Create two models, one without special characters Hello and another with Héllo, register both with the admin website.

Then, when you click on Hello in the admin website it highlights in yellow in the sidebar, but it is not the case for Héllo, which remains white/gray. This is true for any number of models in any order, only those with an accent will exhibit this behaviour.

Change History (6)

comment:1 Changed 3 months ago by Mariusz Felisiak

Cc: Tom Carrick Matthias Kestenholz added
Triage Stage: UnreviewedAccepted

Thanks for the report! Adding urlencode fixes this issue for me:

diff --git a/django/contrib/admin/templates/admin/app_list.html b/django/contrib/admin/templates/admin/app_list.html
index ea4a85bd0b..e7517ea698 100644
--- a/django/contrib/admin/templates/admin/app_list.html
+++ b/django/contrib/admin/templates/admin/app_list.html
@@ -2,15 +2,15 @@
 
 {% if app_list %}
   {% for app in app_list %}
-    <div class="app-{{ app.app_label }} module{% if app.app_url in request.path %} current-app{% endif %}">
+    <div class="app-{{ app.app_label }} module{% if app.app_url in request.path|urlencode %} current-app{% endif %}">
       <table>
         <caption>
           <a href="{{ app.app_url }}" class="section" title="{% blocktranslate with name=app.name %}Models in the {{ name }} application{% endblocktranslate %}">{{ app.name }}</a>
         </caption>
         {% for model in app.models %}
-          <tr class="model-{{ model.object_name|lower }}{% if model.admin_url in request.path %} current-model{% endif %}">
+        <tr class="model-{{ model.object_name|lower }}{% if model.admin_url in request.path|urlencode %} current-model{% endif %}">
             {% if model.admin_url %}
-              <th scope="row"><a href="{{ model.admin_url }}"{% if model.admin_url in request.path %} aria-current="page"{% endif %}>{{ model.name }}</a></th>
+              <th scope="row"><a href="{{ model.admin_url }}"{% if model.admin_url in request.path|urlencode %} aria-current="page"{% endif %}>{{ model.name }}</a></th>
             {% else %}
               <th scope="row">{{ model.name }}</th>
             {% endif %}

comment:2 Changed 3 months ago by Matthias Kestenholz

I have never used model names with umlauts or accents; I knew that Python 3 supports using unicode codepoints in class names too but I've never had a reason for using anything but [A-Za-z0-9_] when naming Django model classes (or any Python identifiers, for that matter)

I don't know that area of the Django admin very well but the fix looks correct to me. At least if Django even wants to support umlauts/accents in model names (and therefore also database table names, maybe?) at all.

request.path only exists in the admin/app_list.html template in django/contrib/admin/templates/ so I'd assume that the proposed patch fixes this problem.

TLDR: LGTM but I wouldn't ever use accents or umlauts in class names.

comment:3 Changed 3 months ago by Hasan Ramezani

Has patch: set
Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:4 Changed 3 months ago by sarath ak

I have tested in my local worked without error

can't replicate issue

comment:5 Changed 3 months ago by Mariusz Felisiak

Triage Stage: AcceptedReady for checkin

comment:6 Changed 3 months ago by Mariusz Felisiak <felisiak.mariusz@…>

Resolution: fixed
Status: assignedclosed

In d1216e12:

Fixed #33051 -- Fixed highlighting the current model in admin's sidebar with non-ASCII model names.

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