Django

College Event Registration Website

CampusConnect Introduction and Setup Configuring settings file for template, static and media files Setting up Tailwind Creating Custom User Model Creating Super User for CampusConnect Registering Custom User Model Search and Filter for User Model Serving Media Files During Development Categorizing Departments Linking Department with HOD Creating Abstract Model for Event Creating Model for Workshop Customizing Admin Page for Workshop Update in Model AbstractEvent Adding Instructor for Workshop Instructor Model Admin Page Adding Poster Field in Abstract Event Providing Access to HOD Access Update for HOD Restricting HOD Access to Particular Department AbstractEvent On Spot Registration Field Creating Workshop Object Creating and Linking Home Page Displaying Workshop on Home Page Styling Home Page Adding Workshop Detail Page Link Workshop Detail Page Workshop Detail Page Styling Workshop Instructor Details Workshop Detail Contact Contact Admin Page Many to Many Field for Contact Displaying Contact on Workshop Detail Page Adding Title for Workshop Detail Page Adding Gallery for Workshop Workshop Gallery Admin Page Displaying Gallery Images on Website Through Context Displaying Gallery Images on Website through template tags Authentication for users User Registration User Registration Submission Logout Functionality For User Login Functionality for User Model For Workshop Registration Workshop Registration Admin Page Register Workshop Function Register Button in Workshop Page Validations Before Workshop Registration Workshop Registration Closed Validaiton User Already Registered for Workshop Validation Workshop Registration Report From Admin Page Export using Library in Django Admin Extending Abstract Event for Hackathons

Creating Custom Command

To create a custom command in Django, you first need to create an app. Click here to see how to create an app.

After you create an app inside your project, create a management folder inside the app directory and commands folder inside the management folder. Inside the commands folder create a python file with the name, you want to give to your command. For example, if you want to create a command named my_command, then the name of the python file will be my_command.py alt text You should edit the my_command.py file as follows to create a command:

from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = 'A Custom Management command'

    def handle(self, *args, **kwargs):
        self.stdout.write(self.style.SUCCESS('Custom command executed successfully'))
  • BaseCommand is a base class provided by Django for creating custom management commands.
  • The help attribute is a short description of what the custom command does. It provides information that can be displayed to users when they run python manage.py help <command_name> to get help on the command.
  • The handle method is the entry point for your custom command's logic. When you run the custom command, Django will invoke this method. It accepts two arguments, *args and **kwargs.
  • Inside the handle method, the self.stdout.write method is used to display output to the console.

You can run your command as follows :

python manage.py my_command

alt text Users can know about your command using --help :

python manage.py my_command --help

The following command would give an output like this :

usage: manage.py my_command [-h] [--version] [-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH]
                            [--traceback] [--no-color] [--force-color] [--skip-checks]

A Custom Management command

optional arguments:
  -h, --help            show this help message and exit
  --version             Show program's version number and exit.
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided,
                        the DJANGO_SETTINGS_MODULE environment variable will be used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions.
  --no-color            Don't colorize the command output.
  --force-color         Force colorization of the command output.
  --skip-checks         Skip system checks.

Also, you can pass arguments in your command as follows :

class Command(BaseCommand):
    help = 'A Custom Management command'

    def add_arguments(self, parser):
        parser.add_argument('--my_option', type=int, help='An example option')

    def handle(self, *args, **kwargs):
        my_option_value = kwargs['my_option']
        self.stdout.write(self.style.SUCCESS(f'Custom command executed with option: {my_option_value}'))

This code accepts an argument named my_option of type int.

Now the argument can be passed while executing the command as follows :

python manage.py my_command --my_option 10

alt text Now if you give help, you will be able to see the my_option in the arguments as well.

usage: manage.py my_command [-h] [--my_option MY_OPTION] [--version] [-v {0,1,2,3}] [--settings SETTINGS]
                            [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color] [--skip-checks]

A Custom Management command

optional arguments:
  -h, --help            show this help message and exit
  --my_option MY_OPTION
                        An example option
  --version             Show program's version number and exit.
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output
  --settings SETTINGS   The Python path to a settings module, e.g. "myproject.settings.main". If this isn't provided,
                        the DJANGO_SETTINGS_MODULE environment variable will be used.
  --pythonpath PYTHONPATH
                        A directory to add to the Python path, e.g. "/home/djangoprojects/myproject".
  --traceback           Raise on CommandError exceptions.
  --no-color            Don't colorize the command output.
  --force-color         Force colorization of the command output.
  --skip-checks         Skip system checks.