(For beginners) Enabling Django on Mac

This article summarizes the steps for beginners to use Django. I would like to take this opportunity to summarize it in an article.

Environment

It was tested in the following environment.

  • MacBook Pro
  • macOS Monterey 12.3.1

What is Django?

Django is a web framework that runs on Python. (Wikipedia)

First of all, the goal is to get Django running.

Install Homebrew

Django runs on Python, so let’s use Python.

There seem to be several ways to install Python on Mac. This time I chose to install using Homebrew.

Homebrew (https://brew.sh/index_ja)

To install, just copy and paste the code at the top of Homebrew and enter it in the terminal.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

After entering the administrator password, the installation will begin. When the command prompt returns with “Installation successful!” displayed, Homebrew installation is complete.

Install Python

Now that we have Homebrew installed, let’s install Python.

brew install python3

Python3.9 is now installed.

Pass the path of the installed Python

tomokatsu@snow-under-MacBook-Pro ~ % where python3   
/opt/homebrew/bin/python3
/usr/bin/python3

The installed Python seems to be installed under /opt/homebrew. So let’s pass. If it is not set, the following will be displayed.

tomokatsu@snow-under-MacBook-Pro ~ % which python3  
/usr/bin/python3

Define the path in .zsh_profile to pass the path.

vi ~/.zsh_profile

Once vi starts, write the homebrew path.

export PATH=/opt/homebrew/bin/:$PATH

When I restarted the terminal, python was now the version installed with homebrew.

tomokatsu@snow-under-MacBook-Pro ~ % which python3
/opt/homebrew/bin/python3
tomokatsu@snow-under-MacBook-Pro ~ % python3 --version
Python 3.9.12

Install Django

Create working folder

Create a working folder.

tomokatsu@snow-under-MacBook-Pro ~ % mkdir test
tomokatsu@snow-under-MacBook-Pro ~ % cd test
tomokatsu@snow-under-MacBook-Pro test %

Setting up the Python virtual environment

Python seems to be able to use a virtual environment, and the modules to be installed can be changed for each environment. This time, I created an environment called django.

tomokatsu@snow-under-MacBook-Pro test % python3 -m venv django            
tomokatsu@snow-under-MacBook-Pro test % source ./django/bin/activate
(django) tomokatsu@snow-under-MacBook-Pro test % 

Install Django with pip3

Next, install Django.

(django) tomokatsu@snow-under-MacBook-Pro test % pip3 install django

When the installation is complete, a completion message will appear.

Collecting django
  Using cached Django-4.0.4-py3-none-any.whl (8.0 MB)
Collecting sqlparse>=0.2.2
  Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Collecting asgiref<4,>=3.4.1
  Using cached asgiref-3.5.0-py3-none-any.whl (22 kB)
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.5.0 django-4.0.4 sqlparse-0.4.2
(django) tomokatsu@snow-under-MacBook-Pro test % 

Create a Django project

django-admin startproject You can create a project with

(django) tomokatsu@snow-under-MacBook-Pro ~ % django-admin startproject firstproject
(django) tomokatsu@snow-under-MacBook-Pro test % ls -Fla
total 0
drwxr-xr-x   4 tomokatsu  staff   128  4 15 11:55 ./
drwxr-x---+ 39 tomokatsu  staff  1248  4 15 11:53 ../
drwxr-xr-x   6 tomokatsu  staff   192  4 15 11:55 django/
drwxr-xr-x   4 tomokatsu  staff   128  4 15 11:55 firstproject/

Start Django

Let’s start Django.

local server startup

First, move to the Django folder.

(django) tomokatsu@snow-under-MacBook-Pro test % cd firstproject 
(django) tomokatsu@snow-under-MacBook-Pro firstproject % 

To start the server

python3 manage.py runserver : You can start it with

It seems possible to just use the port number instead. If you omit all of them, the default will be number 8000.

$ python3 manage.py runserver
$ python3 manage.py runserver 8080
(django) tomokatsu@snow-under-MacBook-Pro firstproject % python3 manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
April 15, 2022 - 02:58:03
Django version 4.0.4, using settings 'firstproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[15/Apr/2022 02:58:23] "GET / HTTP/1.1" 200 10697
[15/Apr/2022 02:58:23] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[15/Apr/2022 02:58:23] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 85876
[15/Apr/2022 02:58:23] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 86184
[15/Apr/2022 02:58:23] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 85692
Not Found: /favicon.ico
[15/Apr/2022 02:58:23] "GET /favicon.ico HTTP/1.1" 404 2116

I was able to start it!

I was able to install it and start django. thank you for your hard work.

Related