- Prerequisites
- Install Python 3
- Install Pip
- Install Virtualenv
- Install Postgres
- Install Django
- 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
- Programming
-
Register app to project Go to
project/project/settings.py
addapp
(the app we have just created) toINSTALLED_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 usingsuperuser
.
BOOM__*