Opened 3 years ago

Closed 3 years 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 by Mariusz Felisiak, 3 years ago

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 by Matthias Kestenholz, 3 years ago

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 by Hasan Ramezani, 3 years ago

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

comment:4 by sarath ak, 3 years ago

I have tested in my local worked without error

can't replicate issue

comment:5 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

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