Skip to content

Instantly share code, notes, and snippets.

@t18n
Last active September 6, 2018 19:38
Show Gist options
  • Save t18n/67db924d5610e3b29378229fa3bf6772 to your computer and use it in GitHub Desktop.
Save t18n/67db924d5610e3b29378229fa3bf6772 to your computer and use it in GitHub Desktop.
Quickly roll up Django app.md
  1. Prerequisites
  • Install Python 3
  • Install Pip
  • Install Virtualenv
  • Install Postgres
  • Install Django
  1. Installation
  • Create project
mkdir Django-Postgres-Rest
cd Django-Postgres-Rest
  • Create and activate environment
virtualenv env
source env/bin/activate
  • Create structure
pip install django djangorestframework
pip freeze // See what has installed in the project
pip freeze > requirements.txt // Save dependencies list to file for future installation
pip install -r requirements.txt

django-admin.py startproject project && cd project
  • Create a new app
python manage.py startapp app
  • Migrate initial database
python manage.py migrate
  • Run server
python manage.py runserver
  1. Programming
  • Register app to project Go to project/project/settings.py add app (the app we have just created) to INSTALLED_APPS array to register.

  • Create a model for Post In App > Model.py add these code:

class Post(models.Model) :
  user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
  creation_date = models.DateTimeField(auto_now_add=True)
  post = models.TextField()
  • Register Model to admin page In App > Admin.py
from . import models

# Register your models here.
admin.site.register(models.Post)
  • Migrate Model to database again, then create superuser using command:
./manage.py makemigrate && ./manage.py migrate
./manage.py createsuperuser
  • Open the server, navigate to localhost:__PORT__/admin and login using superuser.

BOOM__*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment