Adding a Command palette to the Django Admin

Project Title : Adding a Command palette to the Django Admin

Student Name : Chathura
Project Mentor : Tom Carrick
Contact : mcchathuer216@gmail.com
, Github

Motivation

In the current state of the django admin, it becomes harder and harder to manage a database when number of admin registered models and apps goes higher.

A Command palette is the best battle tested solution to improve the existing functionality and add more functionality.
It can save time, reduce the complexity, make admin more user friendly while not doing any major changes in existing UI.

Expected improvements in usability

  • Reduced scrolling / page navigation
  • Quick create, edit, delete queries for single objects
  • Quick create, update, delete queries for multiple objects with patterns
  • Custom key combination for existing functionality
  • Custom key combination for custom queries / page navigation

Implementation process

  • Research and identify existing browser key combinations for all modern browsers.
  • Research and recognise most user friendly key combinations
  • Recognise most useful functionalities
  • Map key combinations to suitable functionalities
  • Write relevant js, forms, py codes and test in development environment
  • Marge the code to Django

Any help or advice is appreciated without any problem.

Hi @ChathuraGH :waving_hand:

Thankyou for showing interest in contributing to Django. Looks like a decent start to me. I’d recommend going through the initial work that has been done to add keyboard shortcuts to core.

The next steps would be to create a structured draft proposal with all the details included and share it here for feedbacks from the community and potential mentor. You can refer to my proposal from GSoC 2023 : "Allow moving a model between apps" -- proposal for Google Summer of Code 2023. · GitHub

Looking forward to see the first draft! Good Luck!

cc @tom

1 Like

Thank you @DevilsAutumn for the response :heart:. I will post the proposal here.

For anyone reading the context of this exact post : - The method and code patterns in this post doesn’t work. I wrote them as a initial version only. It turns out the method & code wouldn’t work; most of the code is unnecessary. (as my understanding).

Thanks. :folded_hands::rocket:.

------------------------------------------------
------------------------------------------------

“Google Summer of Code Project Proposal 2025: Adding a Command Palette to the Django Admin Interface”

Table of content

1.Abstract
- 1.1 Overview
- 1.2 Goals
- 1.3 Benefits

  1. The solution

    • 2.1 Create command_palette.html.
    • 2.2 Create a CSS file command_palette.css.
    • 2.3 Create a JavaScript file command_palette.js
    • 2.4 Link command_palette.css and command_palette.js to the command_palette.html
    • 2.5 Create a view in the admin app’s views.py.
    • 2.6 Adding the URL pattern in urls.py.
    • 2.7 Adopt the relevant code from django-admin-keyboard-shortcuts.
  2. Schedule and milestones

  3. About me

1.Abstract

1.1 Overview

The Django Admin interface is a powerful tool for managing application data, but its reliance on manual navigation can slow down power users.
In the current state of the Django admin, it becomes harder and harder to manage a database when the number of admin-registered models and apps goes higher.

A Command palette is the best battle-tested solution to improve the existing functionality and add more functionality.

This project proposes the implementation of a Command Palette, a centralized, keyboard-driven interface for quick access to common admin actions (navigation, object creation, search, etc.).
and
It can save time by reducing clicks, reducing the complexity, and making admin more user-friendly while not making any major changes to the existing UI.

Already, there is some initial work has been done in the django-admin-keyboard-shortcuts repository. But not implemented in the Django core.
So, this project aims to implement a full command palette to the Django admin core.

1.2 Goals

  1. Create a new template command_palette.html in templates/admin directory.
  2. Create a CSS file command_palette.css at static/admin/css/ styling the UI of the Command palette.
  3. Create a JavaScript file command_palette.js at static/admin/js/ containing key bindings and Ajax.
  4. Link command_palette.css and command_palette.js to the command_palette.html.
  5. Create a view in the admin app’s views.py for command execution.
  6. Add the URL pattern in urls.py for the command execution view.
  7. Adopt the code from django-admin-keyboard-shortcuts in relevant development steps.
  8. Users can set up custom key combinations or override.

1.3 Benefits

  • Reduced scrolling / Reduced Manual page navigation
  • Quick create, edit, and delete queries for single objects
  • Quick create, update, and delete queries for multiple objects with patterns
  • Custom key combination for existing functionality
  • Custom key combination for custom queries/page navigation

2.The Solution

  1. Frist we need to create a new template command_palette.html in templates/admin directory and add relevant html UI interface.

  2. At the same time, we need to create a CSS file command_palette.css at static/admin/css/ and add it to the command_palette.html. Then make sure the default UI interface is in proper style for easy recognition in expected visual shape by adding relevant CSS code to the command_palette.css.

  3. After the expected visual shape is achieved, we need to create a JavaScript file command_palette.js at static/admin/js/ containing key bindings and Ajax.
    This command_palette.js should include the code for key binding triggering the UI, key bindings for the specific commands, and Ajax for accessing the view that will be created in the next steps, showing the results received from the server.

  4. Then we need to link command_palette.css and command_palette.js to the command_palette.html.

  5. Then we need to create a view in the admin app’s views.py for actual command execution. This view should include the code for the execution of specific queries, response to relevant results, relevant permissions checks, and relevant logic for proper code execution.

  6. Then we need to link the view by Adding the URL pattern in urls.py for the command execution view.

  7. Create a template tag command_palette_commands that will generate a list of all the required commands.

2.1 Create command_palette.html

We need to create a new template command_palette.html in templates/admin directory and add the relevant HTML UI interface.

{% extends "admin/base_site.html" %}
{% load static %}

{% block extrahead %}
{{ block.super }}
{% endblock %}

{% block content %}
{{ block.super }}
<div id="command-palette" class="command-palette">
    <div class="command-palette-header">
        <input type="text" id="command-search" placeholder="Search commands...">
    </div>
    <div class="command-list">
        <!-- Commands will be populated here via JavaScript -->
    </div>
</div>
{% endblock %}

{% block footer %}
{{ block.super }}
{% endblock %}

Note: Need to use the command_palette_commands template tag to make a list of commands as a javascript list.

2.2 Create a CSS file command_palette.css

Create a CSS file command_palette.css at static/admin/css/ styling the UI of the Command palette.

.command-palette {
    display: none;
    position: fixed;
    top: 20%;
    left: 50%;
    transform: translateX(-50%);
    width: 600px;
    max-width: 90%;
    background: white;
    border: 1px solid #ddd;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    z-index: 10000;
}

.command-palette-header {
    padding: 15px;
    border-bottom: 1px solid #eee;
}

#command-search {
    width: 100%;
    padding: 10px;
    font-size: 16px;
}

.command-list {
    max-height: 400px;
    overflow-y: auto;
}

.command-item {
    padding: 10px 15px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.command-item:hover {
    background-color: #f5f5f5;
}

.command-shortcut {
    color: #666;
    font-size: 0.9em;
}

Note: Initially, the command palette is hidden.

2.3 Create a JavaScript file command_palette.js

Create a JavaScript file command_palette.js at static/admin/js/ containing key bindings and Ajax.

document.addEventListener('DOMContentLoaded', function() {
    const palette = document.getElementById('command-palette');
    const searchInput = document.getElementById('command-search');
    const commandList = document.querySelector('.command-list');
    
    function renderCommands() {
...
...
            });
        });
    }
    
    // Toggle command palette
    document.addEventListener('keydown', (e) => {
        if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
            e.preventDefault();
            palette.style.display = 'block';
            searchInput.focus();

        }
    });
    
    // Close palette on ESC
    document.addEventListener('keydown', (e) => {
        if (e.key === 'Escape') {
            palette.style.display = 'none';
        }
    });
    
    // Search functionality
    searchInput.addEventListener('input', (e) => {
        const searchTerm = e.target.value.toLowerCase();
...
...

    });
});

Note: Need to use the commands list generated inside command_palette.html and config the ajax requests accordingly.

2.4 Link command_palette.css and command_palette.js to the command_palette.html

{% extends "admin/base_site.html" %}
{% load static %}

{% block extrahead %}
{{ block.super }}
<link rel="stylesheet" href="{% static 'admin/css/command_palette.css' %}">
{% endblock %}
...
...
{% block footer %}
{{ block.super }}
<script src="{% static 'admin/js/command_palette.js' %}"></script>
{% endblock %}

2.5 Create a view in admin app’s views.py

We need to create a view in the admin app’s views.py for the command execution.

from django.contrib.admin.sites import site
from django.http import JsonResponse
from django.contrib.auth.decorators import staff_member_required

@staff_member_required
def command_palette(request,command):
    
    results=Run(command)
    
    return JsonResponse({results})

2.6 Adding the URL pattern in urls.py

We are adding the URL pattern in urls.py to link the view to requests.


from django.urls import path
from .views import command_palette

urlpatterns = [
    path('admin/command_palette/<path:command>', command_palette, name='command_palette'),
    # ... other URLs
]

2.7 Allow Custom key combinations & override

Users can set up custom key combinations or override existing combinations with config dictionary from the settings.py.

3.Schedule and milestones

3.1 Discuss & Research(1-2 week)

  • Discuss the methodology and procedures with senior developers or Mentors.
  • Research and identify existing browser key combinations for all modern browsers.
  • Research and recognize the most user-friendly key combinations.
  • Recognise the most useful functionalities.
  • Map key combinations to suitable functionalities.
  • Write basic HTML,CSS,JS(1st versions of command_palette.html , command_palette.css & command_palette.js).
  • Create command_palette_commands template tag.(1st version)
  • Create the views.py & URL pattern in urls.py(1st versions).

3.2 1st generation command run – first milestone

3.2.1 Modify the views.py & command_palette.html and command_palette_commands template tag. (1-2 week)
  • Modify command_palette_commands to generate 1st generation commands(without any db changes).
  • Modify the views.py for the 1st generation commands execution, redirects, and queries.
  • Modify command_palette.html to respond to app-specific, navigational, search commands.

3.3 2nd generation command run – second milestone

3.3.1 2nd generation commands and custom key combinations(2-4 weeks)
  • Modify the command_palette_commands to generate 2nd generation commands.
  • Modify command_palette.html to show command execution results in a result container.
  • Let users define a list of key combinations and let them activate and deactivate the global availability of any key combination.

3.4 User guide, Testing and documentation

3.4.1 User guide(1 week)
  • Modify command_palette.html and add a user guide on how to use the command palette.
3.4.2 Testing and documentation(1.5-2 weeks)
  • Testing for correct command generation, and correct command execution is needed to be done.
  • Documentation for key combinations and use of command selection patterns is done.

4.About me

My name is Chathura and I am a self-taught Django developer. Studied 2 years in the Colombo University, Sir Lanka. I started my coding journey 6 years ago with Python, JavaScript, HTML, and CSS. I have been using Django for 5 years in various projects.

4.1 Personal Details

  • Email : mcchathuer216@gmail.com
  • Timezone : Indian Standard Time (UTC + 5:30)
  • Primary Language : English
  • Country of Residence : Sir Lanka
  • Github Profile : ChathuraGH
  • Stack Profile : Pycm

Hi,

Thanks for the proposal. This is a good start.

What I’m missing a bit from the goals is which commands exactly you intend to introduce. I’d also like to know if you have any ideas for implementation for any of the more difficult commands.

1 Like

Ok. I will add exact commands and initial execution procedures that I have already mapped.

Hi, quick question, is it okay to post that in here publicly, because this is a competition?

Also, what happens when multiple people try for the same project proposal?

But I see you are also a GSoC contributor this year?

No no, I like it. I was just expecting an answer from a django mentor. It’s just that. Nothing more. I will accept any helpful opinion from anyone, including you. Thank you. :+1:

Hi @adyaprasad

I really appreciate you helping other with their queries and guiding them forward but I would recommend not to use words like “crazy” and “desperate”. These words sounds kind of rude in my opinion.

Also try to keep the conversations little professional and reply only where it is really required, else there is a high chance of you getting ignored. There is no limit or restriction on sending messages in the forum but I am just suggesting it on the basis of past/other conversations here.

Cheers!

1 Like

Hi @ChathuraGH ,

You can post the draft proposal here (not the final one) to give the idea about your thought process, research you have done and potential solution. Based on the suggestions from the mentors, try to make changes and submit the final proposal directly on the GSoC website.

When multiple people submit proposals for the same project, Along with the quality of their proposal, we consider other factors like candidate’s past contributions to Django, their professionalism, communication skills etc.

I hope that helps.

1 Like

Thank you for the response. :+1:

@adyaprasad I appreciate that you’re enthusiastic about your proposal and want it to get attention and constructive feedback. I notice you’ve posted similar messages in several other topics here. I’d encourage you to stop doing that. (It may attract attention, but probably not the positive attention you want.)

A forum like this works best when each thread can focus on a specific topic. This one is meant to be about adding a command palette to the Django Admin, but right now roughly 1/3 of its posts are about something else. Off-topic posts about unrelated proposals add noise, which makes it harder for readers to follow the conversation.

[Also, I’m aware this reply is contributing to the problem. Perhaps a moderator will remove it as off topic.]

1 Like

My idea that aims to solve complex commands is to use four different command start characters(reserved) to differentiate the query type. Like, “?” for search queries and etc. Then we suggest avaliable registered models for users to select, then we allow to type the query string. We use “|” separate each part of the full command.

Example :- “?|post_model|query string”

So, user has to initiate the command palette by “ctrl+k” and then type “?” to make it a search command, then we suggest avaliable models to pick, opon model selection, we allow to type query string.

So, full command will be,

“ctrl+k” + “?” + “model” + “query string” + “Enter” .

Are the above commands you mean by

?

Hi @tom, is this a good method or bad method? I just thought it intuitively. I can’t say it’s the best method or not. Please, if you can guide me. :eyes:

Ctrl+K + ? + post + hello

?|post|hello

Or, is it something we should discuss in the development pace?

I’m open for changing my opinion in the best direction. Thanks.

I’m interested in the technical implementation more. The UX is important also but I think not as necessary to evaluate a proposal. I’m not a big fan though, since most command palette commands will be around searching things it seems not quite right. I would advice taking a look at GitHub’s and thinking about how orgs and repos might translate into apps and models. Not everything will fit into this paradigm but not everything will fit into a command palette either.

All things to think about.

1 Like

You mean the actual code? Django? Or javascript? It’s little bit unclear. If you can help to understand.

What do you mean? If you can explain little bit.

:folded_hands:

I just started coding my own testing version of a Command palette to try my commands and to check the feasibility of the patterns and commands, because of your above reply.

I just tested most of the commands in my local environment. It looks very cool. If you like to see, I can send you the level 1 code. It includes UI, working commands, working key bindings etc. :blush:

Hello again tom, I just tested all the commands on my environment with UI and results. If you can let me know what are the things I should consider adding to my proposal. Thanks again.

I’m referring to your proposed syntax of ? and |.

I’m afraid I don’t really have time to repeatedly review draft proposals but of course the more working stuff is in your proposal, the better.

1 Like