Project setup

Create a new Django project named tutorial, then start a new app called quickstart.

  1. # Create the project directory
  2. mkdir tutorial
  3. cd tutorial
  4. # Create a virtual environment to isolate our package dependencies locally
  5. python3 -m venv env
  6. source env/bin/activate # On Windows use `env\Scripts\activate`
  7. # Install Django and Django REST framework into the virtual environment
  8. pip install django
  9. pip install djangorestframework
  10. # Set up a new project with a single application
  11. django-admin startproject tutorial . # Note the trailing '.' character
  12. cd tutorial
  13. django-admin startapp quickstart
  14. cd ..

The project layout should look like:

  1. $ pwd
  2. <some path>/tutorial
  3. $ find .
  4. .
  5. ./manage.py
  6. ./tutorial
  7. ./tutorial/__init__.py
  8. ./tutorial/quickstart
  9. ./tutorial/quickstart/__init__.py
  10. ./tutorial/quickstart/admin.py
  11. ./tutorial/quickstart/apps.py
  12. ./tutorial/quickstart/migrations
  13. ./tutorial/quickstart/migrations/__init__.py
  14. ./tutorial/quickstart/models.py
  15. ./tutorial/quickstart/tests.py
  16. ./tutorial/quickstart/views.py
  17. ./tutorial/settings.py
  18. ./tutorial/urls.py
  19. ./tutorial/wsgi.py

It may look unusual that the application has been created within the project directory. Using the project's namespace avoids name clashes with external modules (a topic that goes outside the scope of the quickstart).

Now sync your database for the first time:

  1. python manage.py migrate

We'll also create an initial user named admin with a password of password123. We'll authenticate as that user later in our example.

  1. python manage.py createsuperuser --email admin@example.com --username admin

Once you've set up a database and the initial user is created and ready to go, open up the app's directory and we'll get coding…