Skip to content

arsho/django-custom-command

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django Custom Command Example with Django Docs Polls Application

This is the code repository of the following tutorial: https://arshovon.com/blog/django-custom-command/

Directory Structure

.
├── mysite
│   ├── manage.py
│   ├── mysite
│   │   ├── asgi.py
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   └── polls
│       ├── admin.py
│       ├── apps.py
│       ├── __init__.py
│       ├── management
│       │   └── commands
│       │       ├── command_utils.py
│       │       ├── insert_dummy_questions.py
│       │       └── questions.csv
│       ├── migrations
│       │   ├── 0001_initial.py
│       │   └── __init__.py
│       ├── models.py
│       ├── templates
│       │   └── polls
│       │       ├── detail.html
│       │       ├── index.html
│       │       └── results.html
│       ├── tests.py
│       ├── urls.py
│       └── views.py
└── requirements.txt

Installing Requirements

  • Python version: 3.6+
  • Create virtual environment:
    python3 -m venv venv
    
  • Activate virtual environment:
    source venv/bin/activate
    
  • Install required packages:
    pip install -r requirements.txt
    

Database Migration

  • Create migration:
    python manage.py makemigrations
    
  • Migrate migrations:
    python manage.py migrate
    
  • Create super user:
    python manage.py createsuperuser
    

Run the project

  • Run the project from the root directory of Django project:
    python manage.py runserver
    

Run Django Custom Command

  • Run Django custom command help context:

    python manage.py insert_dummy_questions --help
    

    alt Django Custom Command Help context

  • Run Django custom command:

    python manage.py insert_dummy_questions questions.csv
    

    alt Django Custom Command

  • After running Django custom command the CSV file data is inserted into database:

alt Polls page

Footnote

  • I have used Django Docs Example Polls Application to demonstrate Django Custom Command.
  • Utility methods of custom command files are kept in a separate file in mysite/polls/management/commands/command_utils.py. Then in custom command file I have imported required methods like: from .command_utils import get_csv_file

Reference

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published