<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Django | Tomokatsu Yukishita | yre.jp</title><link>https://yre.jp/en/tag/django/</link><atom:link href="https://yre.jp/en/tag/django/index.xml" rel="self" type="application/rss+xml"/><description>Django</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-US</language><lastBuildDate>Fri, 15 Apr 2022 00:00:00 +0000</lastBuildDate><image><url>https://yre.jp/media/icon_hufbc159bd6ce6a866189b19a79c0d0f51_12846_512x512_fill_lanczos_center_3.png</url><title>Django</title><link>https://yre.jp/en/tag/django/</link></image><item><title>How to Install Django on Mac with Homebrew and Run a Local Development Server</title><link>https://yre.jp/en/post/jango_mac/</link><pubDate>Fri, 15 Apr 2022 00:00:00 +0000</pubDate><guid>https://yre.jp/en/post/jango_mac/</guid><description>&lt;p>This guide walks through installing Django on macOS step by step, from installing Python via Homebrew through to launching a local development server. Written for beginners.&lt;/p>
&lt;h2 id="test-environment">Test Environment&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Item&lt;/th>
&lt;th>Version&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Machine&lt;/td>
&lt;td>MacBook Pro (Apple Silicon)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>OS&lt;/td>
&lt;td>macOS Monterey 12.3.1&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;h2 id="what-is-django">What Is Django?&lt;/h2>
&lt;p>&lt;a href="https://www.djangoproject.com/" target="_blank" rel="noopener">Django&lt;/a> is an open-source web framework written in Python. It comes with built-in admin interfaces, authentication, and an ORM (database abstraction layer), making it possible to build web applications efficiently without reinventing the wheel. Notable large-scale users include Instagram and Disqus.&lt;/p>
&lt;p>The goal of this article is to get Django installed and running locally.&lt;/p>
&lt;h2 id="step-1-install-homebrew">Step 1: Install Homebrew&lt;/h2>
&lt;p>Django runs on Python, so the first step is getting Python onto the Mac. There are several ways to do this; using &lt;a href="https://brew.sh/" target="_blank" rel="noopener">Homebrew&lt;/a> is the most practical because it also manages updates and other packages.&lt;/p>
&lt;p>Paste the following into Terminal and press Enter:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">/bin/bash -c &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="k">$(&lt;/span>curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh&lt;span class="k">)&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Enter your administrator password when prompted. When you see &lt;code>Installation successful!&lt;/code>, Homebrew is ready.&lt;/p>
&lt;h2 id="step-2-install-python">Step 2: Install Python&lt;/h2>
&lt;p>With Homebrew installed, install Python:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">brew install python3
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Confirm the installation:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">python3 --version
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>You should see output like &lt;code>Python 3.x.x&lt;/code>.&lt;/p>
&lt;h2 id="step-3-set-the-path-for-homebrew-python">Step 3: Set the PATH for Homebrew Python&lt;/h2>
&lt;p>Homebrew installs Python under &lt;code>/opt/homebrew/bin/&lt;/code>. To make sure this version takes priority over the system Python at &lt;code>/usr/bin/python3&lt;/code>, you need to set your PATH.&lt;/p>
&lt;p>Check which Python is currently active:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">which python3
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>If the output is &lt;code>/usr/bin/python3&lt;/code>, the PATH needs to be configured. Add the Homebrew path to &lt;code>~/.zprofile&lt;/code> (this file is sourced automatically at login on macOS Monterey and later with zsh):&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s1">&amp;#39;export PATH=/opt/homebrew/bin/:$PATH&amp;#39;&lt;/span> &amp;gt;&amp;gt; ~/.zprofile
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Restart Terminal, then verify:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">which python3
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Expected: /opt/homebrew/bin/python3&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="step-4-install-django">Step 4: Install Django&lt;/h2>
&lt;h3 id="create-a-working-directory">Create a Working Directory&lt;/h3>
&lt;p>Create a project folder and move into it:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">mkdir myproject &lt;span class="o">&amp;amp;&amp;amp;&lt;/span> &lt;span class="nb">cd&lt;/span> myproject
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="set-up-a-python-virtual-environment">Set Up a Python Virtual Environment&lt;/h3>
&lt;p>A &lt;strong>virtual environment&lt;/strong> (venv) creates an isolated Python environment per project, allowing different projects to use different package versions without conflicts. This is standard practice for Python development.&lt;/p>
&lt;p>Create and activate a virtual environment named &lt;code>django&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">python3 -m venv django
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">source&lt;/span> ./django/bin/activate
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>When the virtual environment is active, &lt;code>(django)&lt;/code> appears at the start of your prompt.&lt;/p>
&lt;h3 id="install-django-with-pip">Install Django with pip&lt;/h3>
&lt;p>With the virtual environment active, install Django:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">pip3 install django
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>When you see &lt;code>Successfully installed django-x.x.x&lt;/code>, the installation is complete.&lt;/p>
&lt;h2 id="step-5-create-a-project-and-start-the-server">Step 5: Create a Project and Start the Server&lt;/h2>
&lt;h3 id="create-the-django-project">Create the Django Project&lt;/h3>
&lt;p>Use &lt;code>django-admin startproject&lt;/code> to generate a project scaffold:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">django-admin startproject firstproject
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">cd&lt;/span> firstproject
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 id="start-the-local-development-server">Start the Local Development Server&lt;/h3>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-shell" data-lang="shell">&lt;span class="line">&lt;span class="cl">python3 manage.py runserver
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>A successful start looks like this:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl">Watching for file changes with StatReloader
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Performing system checks...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">System check identified no issues (0 silenced).
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Run &amp;#39;python manage.py migrate&amp;#39; to apply them.
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Django version 4.0.4, using settings &amp;#39;firstproject.settings&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Starting development server at http://127.0.0.1:8000/
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">Quit the server with CONTROL-C.
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;blockquote>
&lt;p>The &lt;code>unapplied migration(s)&lt;/code> warning means Django&amp;rsquo;s internal database (used for the admin panel and authentication) has not been initialized yet. You can safely ignore it for a quick test. Run &lt;code>python3 manage.py migrate&lt;/code> later to clear it.&lt;/p>
&lt;/blockquote>
&lt;p>Open &lt;code>http://127.0.0.1:8000/&lt;/code> in a browser. If you see the Django welcome page, everything is working.&lt;/p>
&lt;h2 id="summary">Summary&lt;/h2>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Step&lt;/th>
&lt;th>Command&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Install Homebrew&lt;/td>
&lt;td>&lt;code>/bin/bash -c &amp;quot;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)&amp;quot;&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Install Python&lt;/td>
&lt;td>&lt;code>brew install python3&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Configure PATH&lt;/td>
&lt;td>&lt;code>echo 'export PATH=/opt/homebrew/bin/:$PATH' &amp;gt;&amp;gt; ~/.zprofile&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Create virtual environment&lt;/td>
&lt;td>&lt;code>python3 -m venv django &amp;amp;&amp;amp; source ./django/bin/activate&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Install Django&lt;/td>
&lt;td>&lt;code>pip3 install django&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Create project&lt;/td>
&lt;td>&lt;code>django-admin startproject firstproject&lt;/code>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Start server&lt;/td>
&lt;td>&lt;code>python3 manage.py runserver&lt;/code>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>From here, the &lt;a href="https://docs.djangoproject.com/en/stable/intro/tutorial01/" target="_blank" rel="noopener">official Django tutorial&lt;/a> is the best next step — it covers views, models, and templates in a structured way.&lt;/p></description></item></channel></rss>