Activate Django's manage.py commands completion in Bash in 2 steps
Use TAB key complete feature in Linux Bash console
Overview
One of the best features of Django CMS is to have a wide set of commands to manage your web application from console.
One of the things that Linux users miss when working with a clean instance of Django is the ability of pressing the TAB key while writing a command to autocomplete them or presing TAB key twice to see all the available options.
In this tutorial we will enable such feature.
1. Install
Bash completion feature works by having a special script that uses the three builtin commands that are available to manipulate the programmable completion facilities, they are:
compgen
complete
compopt
For each program, there should be a script that uses them to properly enable the autocompletion feature, in Django this file is located in a special directory in main repository, not included in the delivered project by default, it is located at: https://github.com/django/django/master/extras/django_bash_completion
We first start by downloading this file into
~/local/share/django/extras
directory, so in this case mkdir -p ~/local/share/django/extras
would create the needed path.
Then downloading it and posting in the above directory wget -O ~/local/share/django/extras/django_bash_completion.sh https://raw.github.com/django/django/master/extras/django_bash_completion.
$ mkdir -p ~/local/share/django/extras $ wget -O ~/local/share/django/extras/django_bash_completion.sh https://raw.github.com/django/django/master/extras/django_bash_completion --2022-09-09 17:57:29-- https://raw.github.com/django/django/master/extras/django_bash_completion Resolving raw.github.com (raw.github.com)... 185.199.110.133, 185.199.109.133, 185.199.108.133, ... Connecting to raw.github.com (raw.github.com)|185.199.110.133|:443... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: https://raw.githubusercontent.com/django/django/master/extras/django_bash_completion [following] --2022-09-09 17:57:30-- https://raw.githubusercontent.com/django/django/master/extras/django_bash_completion Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.109.133, 185.199.108.133, ... Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 2240 (2,2K) [text/plain] Saving to: ‘/home/marcanuy/local/share/django/extras/django_bash_completion.sh’ /home/marcanuy/local/share/django/ext 100%[=======================================================================>] 2,19K --.-KB/s in 0,003s 2022-09-09 17:57:31 (740 KB/s) - ‘/home/marcanuy/local/share/django/extras/django_bash_completion.sh’ saved [2240/2240]
2. Sourcing the file
Now we need to source the downloaded script, so the autocompletion is loaded into our console.
To make it available in our console we just run source ~/local/share/django/extras/django_bash_completion.sh.
But there is a good chance that there is a virtual environment for each project, so depending in what type of virtual environment you are using you can load it at different places:
2.1 Manual
If you load your virtual environment manually, e.g.: source ~/.virtualenvs/MY-PROJECT/bin/activate
, with a simple script in you
project root like activate.sh
:
#!/bin/bash
source ~/.virtualenvs/MY-PROJECT/bin/activate
source ~/local/share/django/extras/django_bash_completion.sh
And then load your virtual environment by executing source activate.sh
2.2. Using virtualenvwrapper
For virtualenvwrapper, add source ~/local/share/django/extras/django_bash_completion.sh
in the
post_activate.sh
script.
Conclusion
After doing this simple installation, each type we press the TAB key while executing a command with python manage.py TAB it will load the list of available commands or autocomplete accordingly.
Resources
- https://www.tldp.org/LDP/abs/html/tabexpansion.html
- https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html
- August 1, 2023
- How to create a reusable Django app and distribute it with PIP or publish to pypi.orgJune 29, 2021
- How To Serve Multiple Django Applications with uWSGI and Nginx in Ubuntu 20.04October 26, 2020
- How to add favicon to Django in 4 stepsSeptember 3, 2020
- Categories in Django with BreadcrumbsAugust 30, 2020
- How To Migrate From SQLite To PostgreSQL In Django In 3 stepsAugust 28, 2020
- Practical guide to internationalize a Django app in 5 steps.August 24, 2020
- Disable new users singup when using Django's allauth packageSeptember 3, 2019
- How to add ads.txt to Django as requested by Google AdsenseAugust 30, 2019
- Have multiple submit buttons for the same Django formJuly 2, 2019
- Better Testing with Page Object Design in DjangoMay 1, 2019
- Generating slugs automatically in Django without packages - Two easy and solid approachesFebruary 14, 2019
- How to set up Django tests to use a free PostgreSQL database in HerokuFebruary 13, 2019
- Dynamically adding forms to a Django FormSet with an add button using jQueryFebruary 6, 2019
- Use of Django's static templatetag in css file to set a background imageFebruary 1, 2019
- Activate Django's manage.py commands completion in Bash in 2 steps
- Sending Emails with Django using SendGrid in 3 easy stepsJanuary 9, 2019
- Adding Users to Your Django Project With A Custom User ModelSeptember 21, 2018
- Setting Up A Factory For One To Many Relationships In FactoryboyApril 17, 2018
- Generate UML class diagrams from django modelsMarch 24, 2018
- Set Up Ubuntu To Serve A Django Website Step By StepJuly 3, 2017
- Django Project Directory StructureJuly 16, 2016
- How to Have Different Django Settings for Development and Production, and environment isolationJune 10, 2016
- Django OverviewJune 2, 2016
Django Forms
- Adding a Cancel button in Django class-based views, editing views and formsJuly 15, 2019
- Using Django Model Primary Key in Custom Forms THE RIGHT WAYJuly 13, 2019
- Django formset handling with class based views, custom errors and validationJuly 4, 2019
- How To Use Bootstrap 4 In Django FormsMay 25, 2018
- Understanding Django FormsApril 30, 2018
- How To Create A Form In DjangoJuly 29, 2016
Articles
Subcategories
Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.
Powered by SimpleIT Hugo Theme
·