Skip to content

Creating Django Project and App

Part 3 of 8 in the series Django REST Framework




Assuming you have python environment we created in last post already activated, let’s go ahead and create Django Project and App for our REST API example.

(venv) $ mkdir backend
(venv) $ cd backend

# Create Django Project called "restapi"
(venv) $ django-admin startproject restapi
(venv) $ cd restapi/

#Inside "restapi" directory, Create Django App called "quotes"
(venv) $ django-admin startapp quotes

(venv) $ cd ..
(venv) $ pwd
<your_path>/backend
(venv) $ find .
.
./restapi
./restapi/quotes
./restapi/quotes/migrations
./restapi/quotes/migrations/__init__.py
./restapi/quotes/models.py
./restapi/quotes/__init__.py
./restapi/quotes/apps.py
./restapi/quotes/admin.py
./restapi/quotes/tests.py
./restapi/quotes/views.py
./restapi/restapi
./restapi/restapi/asgi.py
./restapi/restapi/__init__.py
./restapi/restapi/settings.py
./restapi/restapi/urls.py
./restapi/restapi/wsgi.py
./restapi/manage.py

Now sync the database for the first time.

(venv) $ cd restapi/
(venv) $ ls
manage.py       quotes          restapi
(venv) $ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK
(venv) $ ls
db.sqlite3      manage.py       quotes          restapi
(venv) $ 

Django by-default uses sqllite3 and you can see after DB sync, db.sqlite3 is created.

Let’s create superuser also, we’ll authenticate with that user later.

(venv) $ python manage.py createsuperuser --email admin@example.com --username admin
Password: 
Password (again): 
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
(venv) $ 

Now let’s add our app “quotes” and rest_framework app in INSTALLED_APPS settings. This way we are are telling django which apps are available. “rest_framework” is for Django-REST-Framework library which we are going to use mainly for our API interface.

“quotes” is the name of app we created. Replace it with whatever name you have used for your app.

(venv) $ vi restapi/settings.py 
# Add "rest_framework" and "quotes" in INSTALLED_APPS list.
# Make sure about comma after last entry.
Example :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'quotes',

]

You can get all the files related to the examples here : https://github.com/hpin2019/coding-sessions/tree/master/drf-tutorial-1

In next post, we will create DB models and Serializers .

Series Navigation<< Installing Django Rest Framework on macOSDjango REST Framework – Models and Serializers >>
Published inDjango

One Comment

  1. Great content! Super high-quality! Keep it up! 🙂

Leave a Reply

Your email address will not be published.

%d bloggers like this: