Udit Vashisht
Author: Udit Vashisht


Best way to start a Django project (with Github Integration)

  • Aug. 6, 2019, 3:06 p.m.
  • 4 minutes read
  • 362 Views
Best way to start a Django project  (with Github Integration)

There are dozens of ways, which developers use to start their Django projects. Possibly, none of them would be a wrong way, but in this post we will be telling you a way which is very helpful and suitable not only in development but also in production (deployment). Not only this, in this post we will teach you how to integrate Git/Github with your project from the very beginning.

Creating the Project Directory

So, let’s get to the work. First of all, I will suggest you to choose a location on your machine (Windows/ Mac), where you want to save most of your projects and cd to that directory. Then create a new directory and cd into that:

$ mkdir dev
$ cd dev

Now, this dev folder will host all of your individual projects. Inside this dev folder create a new directory (with the name of your project/app) and cd into that. This directory will be referred to as your project directory:-

$ mkdir newproject && cd newproject

Creating and activating the Virtual Environment

Install virtual environment and activate it.

$ virtualenv -p python3 .
# or
$ virtualenv .

# Activate on MACOS/Linux
$ source bin/activate

# Activate on Windows
.\Scripts\activate

Create a new directory with name ‘src’

$ mkdir src && cd src

Installing Django and starting the project

Use pip to install django

$ pip install django

Now inside the src directory create the project

$ django-admin.py startproject newproject .

You must note the following two things.

  1. We have started a project with name ‘newproject’ and same is the name of the project directory (and also the virtualenv).
  2. There is a trailing . (dot).

Creating local Git repository

Browse to the project folder (i.e. newproject) and run the following command. This will initialize an empty Git repository.

$ git init

Now create a file called .gitignore in the project directory. The function of this file is that, it will not add the folders\files mentioned (pattern) in it to the git repository. This is used for not adding venv, env, temp, db files to the repository.

You can download the standard .gitignore file from here.

Creating a remote Git repository on Github

Open github, log in to your account and create a new repository. Add the name (preferably project name) and description of the repository and create it.

Screenshot 2019-08-06 at 3.36.16 PM.png

You won’t like to share your important credentials on the github etc. For that we will be using Python Decouple.

$ pip install python-decouple

Create a .env file inside the directory where manage.py file is (i.e. src)

$ nano .env

Open the file and copy the SECRET_KEY from your settings.py

SECRET\_KEY=YOUR\_SECRET_KEY

Add the following two lines in your settings.py

from decouple import config
SECRET_KEY = config('SECRET_KEY')

Now go to the project directory and check git status to see the files that are untracked

$ git status

You will get something like this

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .gitignore
    pip-selfcheck.json
    src/

Now, add the files and make initial commit using the following codes:-

$ git add .
$ git commit -m "Initial Commit"

If you will check git status now, it will show the following:

$ git status

On branch master
nothing to commit, working tree clean

Now add the remote origin using this :

git remote add origin https://github.com/YOUR_USER_NAME/newproject.git

It will ask you for the username and password of the github account and then will push it to Github.

You can check the Github repository here

We also have a one minute video tutorial on our channel


Tags: Django

Related Posts

How to Extend Django User model using AbstractUser?
By Udit Vashisht | 3 months, 1 week ago

Even if the default Django User Model caters to all of your needs, it is highly recommended to use a Custom User Model. Django’s official documentation recommends so. Now there are two ways to create a Custom User Model:-

  1. By extending AbstractBaseUser
  2. By extending ...

    Read More
How to extend Django User model using a proxy model?
By Udit Vashisht | 3 months, 1 week ago

There is no doubt that the built-in authentication system of Django is quite good and caters the common use cases. But sometimes, we do not have our needs met with out-of-the-box defaults. In such a scenario, we can extend the User Model using various methods. In this tutorial, we will ...

Read More
Search