Image for post
Image for post

Creating a Django Project within a Git Repository

Story

Create a New Django Project

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

$ 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."

Written by

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store