Most webapps have a common initial configuration -- and there is no reason to spend hours doing the exact same config over and over for each new project. This is the first in a series of starter Django projects that follow common starting app patterns.
Following this repo will enable you to setup a standard development environment, webapp, and deploy to Heroku in less than 5 minutes and with fewer than ten lines of code (seriously) -- thank you Docker.
Feel free to follow along with this Youtube video, where I walk through the below setup. If you find this repo helpful, please give it a star and/or like the Youtube video and subscribe to our Django in Minutes channel. We'll be building many more starter apps. Thanks so much!
This starter app leverages the following resources, but you don't need to worry about installing and configuring most of these. Just see prerequisites below for what you need on your machine to make this work:
- Docker - for virtual development environment and easy deployment
- Allauth - for user authentication
- Psycogpg - python client for postgres db
- sbadmin2 - A Free Bootstrap Template from Start Bootstrap.
- Heroku - for very easy production deployment.
As a prerequisite please make sure you have the following tools already installed on your machine:
- Git
- Docker
- Docker Compose
- Heroku CLI - Note: you'll need to create a Heroku account and there may be a cost associated with the servers you use.
- VirtualEnv
$ python3 -m virtualenv DjangoInMinutesStarterProject
$ source DjangoInMinutesStarterProject/bin/activate
- Clone this repo
$ git clone git@github.com:NoahFinberg/djangostarterproject.git
Github - You want to set up a new Github repo for this code so you can make changes for your own project. (also needed for deploying to Heroku)
git remote set-url origin <remote> # remote is git url to your repo.
git push origin main
- Build the Docker Container
$ cd djangostarterproject
$ docker-compose up --build
You should see all the resources including the web server startup in your terminal. Now, open up another terminal window in the same directory.
- Populate Postgres DB
$ cd djangostarterproject
$ docker-compose exec web python manage.py migrate
Now refresh localhost:8000
. That should be enough for you to run the starter project locally. Seriously, that's it! One thing to note is if you change some settings in the Dockerfile or docker-compose.yml files, you'll need to rebuild the docker container.
If you'd like to deploy your project to Heroku, that's also pretty simple now. Just use the following commands:
# generates a new Heroku app
heroku create
# creates new postgres db
heroku addons:create heroku-postgresql:hobby-dev
# sets the SECRET KEY in production. You'll need to generate one. You can use - [https://djecrety.ir/](https://djecrety.ir/) or [https://miniwebtool.com/django-secret-key-generator/](https://miniwebtool.com/django-secret-key-generator/) if you'd like.
heroku config:set SECRET_KEY='replace me with a generated secret key'
# deploy to Heroku
git push heroku main
heroku open
Congrats! You now have a pretty great starting point for your Django project.
Here is what your app should look like when you visit localhost:8000
or your live app on Heroku.
With this starter app you can:
-
You can also logout by clicking on your username in the top right corner and selecting the "Logout" item.
# rebuild docker container
docker build .
# start docker container
docker-compose up
# shutdown docker-container
docker-compose down
# generate db migrations
docker-compose exec web python manage.py makemigrations
# run db migrations
docker-compose exec web python manage.py migrate
# start new app
docker-compose exec web python manage.py startapp <app name>
# shell into docker container
docker-compose exec web python manage.py shell
I combined a few great tutorials to setup this starter app:
- The Beginners Guide To Django User Management and User Authentication Using AllAuth by Vikas Gautam
- Integrating Bootstrap to Django by D Khambu
- Django Docker Heroku Tutorial by jbarham
To get up to speed with Django, I'd also recommend checking out the following tutorials that I used to get up to speed myself:
- Python Django - Build a Web App in 60 minutes: Blog Application by Jamie Gullbrand
- Django 1.10 Tutorial by OverIQ.com
I've made this starter application freely available under an MIT license.
Please feel free to use for commercial and non-commercial products alike. I would respectfully ask that you just cite this repo and provide a reference link back to it. If you appreciate this work, please don't forget to give this repo a star!
Although I've been doing web development for more than five years, I've primarily worked with Rails (and a bit of Flask). As a result, please excuse (and point out) any unconventional practices in my Django code. Though it's very similar to Rails, I've only just started learning Django in the last two weeks.
As I build more Django projects, I'll update this repo to reflect best practices. I also plan to build additional stater applications as I begin to recognize more common Django patterns and resources.
I'll also try to build a bunch of basic starter projects like this one, but with different pages and Bootstrap templates. Please check out Start Bootstrap. I'll likely implement some of their "pro" resources. In which case, I'll buy the developer license. If you make use of any of their resources through my starter projects, you'll need to make sure to also buy your own license from them and any other templates sites. This starter app only uses the free template they offer so I don't think they require a license.
I welcome contributors to this project and future series of starter projects. I think helping other developers through this project has the potential to have a huge impact by saving countless development hours. If you'd like to contribute, please fork this project, make a new branch, and make a merge request.
- Style all other allauth template pages located in
templates/account/
using the Boostrap Template. Currenlty only login, logout, and reset password are styled.
Noah Finberg and Jamie Gullbrand