
Creating a Django Project within a Git Repository
Story
When I took my first programming course, we never used Git to manage our versions as development progressed. As a result, I found myself saving multiple versions of my program as I made small tweaks before submitting. This usually meant I had about a dozen poorly name duplicates on my desktop that I had to reintegrate into my development environment. In my rush to completed one of my last programming projects of the semester, I accidentally submitted the wrong version of the assignment. Little did I know that the submitted program version did not run and resulted in an F on the assignment. After classes started enforcing use of Git, managing versions was pretty much trivial and I never had an issue like this again.
Create a New Django Project
Assumptions:
It is assumed that your project directory is called myproject
and has a virtual environment venv
and a requirements.txt
file before using the following commands. If you don't have them that's okay just ignore the parts that contain references to these files.
Navigate into the project directory that you would like to start the in. Make sure your Django
installed virtual environment is activated for the following commands. For the terminal output we will ommit the (venv)
to the right of the cursor for simplicity.
Note: For the last command, don’t forget the period .
at the end of the command. This tells django to use the current directory as the root rather than creating a new one within it.
# Navigate into project directory.
$ cd `myproject`# Activate virtual environment.
$ source venv/bin/activate# Start a new django project.
$ django-admin startproject myproject .
The startproject
command should have resulted in a folder structure like this.
myproject
├── manage.py
├── myproject
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── venv
│ └── ...
└── requirements.txt
Create the Git Repository
Create the git repository.
$ git init
We’ll have to create a .gitignore
file that tells Git to ignore the contents of the virtual environment venv
and python cache files. We don't want our repository to keep track of these files because it would be redundant: the contents of venv
are referenced in requirements.txt
and __pycache__
files are compiled versions of the python files.
Create a file called .gitignore
...
touch .gitignore
… and add the following contents.
venv
*.pyc
If you will be using a database other than SQLite then go ahead and delete the db.sqlite3
, otherwise, add db.sqlite3
as a line in the .gitignore
file as well.
Now commit the remaining files.
# Stage the files.
git add -A# Create the first commit.
git commit -m "Initial commit."