<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rob McBryde</title>
    <description>The latest articles on DEV Community by Rob McBryde (@rob212).</description>
    <link>https://dev.to/rob212</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F437888%2F8d9eab05-a378-45a3-8362-d3ed7f14bc08.jpeg</url>
      <title>DEV Community: Rob McBryde</title>
      <link>https://dev.to/rob212</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rob212"/>
    <language>en</language>
    <item>
      <title>Django Secret Key</title>
      <dc:creator>Rob McBryde</dc:creator>
      <pubDate>Mon, 15 Jan 2024 12:07:02 +0000</pubDate>
      <link>https://dev.to/rob212/django-secret-key-3ni3</link>
      <guid>https://dev.to/rob212/django-secret-key-3ni3</guid>
      <description>&lt;p&gt;When you start a new Django project, your &lt;code&gt;settings.py&lt;/code&gt; file contains a 'SECRET_KEY' entry by default. &lt;/p&gt;

&lt;p&gt;Both the comments and the secret key values itself both indicate that this is insecure and should not be exposed.&lt;/p&gt;

&lt;p&gt;The SECRET_KEY' is a random 50-character string generated each time &lt;code&gt;startproject&lt;/code&gt; is run. It is used to provide cryptographic signing in our project. &lt;/p&gt;

&lt;p&gt;When working on a Django project that you intend to place into a production environment, you should move this secret key into a &lt;code&gt;.env&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;I have used the &lt;code&gt;environs&lt;/code&gt; package to manage my environment variables, &lt;a href="https://dev.to/rob212/django-environment-variables-f2n"&gt;which I have previously written about&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an .env file
&lt;/h2&gt;

&lt;p&gt;Create a file named &lt;code&gt;.env&lt;/code&gt; in the root directory of your project and in it paste in your secret key from your &lt;code&gt;settings.py&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SECRET_KEY=django-insecure-egyi&amp;amp;vxcm6kkpkaa&amp;amp;wnw0+&amp;amp;ps6%4-s@&amp;amp;c=+891+jfu8j5*adz4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note there are no spaces in the .env file&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now update your &lt;code&gt;settings.py&lt;/code&gt; file to read your secret key from your environment variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# settings.py file
SECRET_KEY = env.str("SECRET_KEY")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Regenerating your secret key
&lt;/h2&gt;

&lt;p&gt;Even though our SECRET_KEY is out of our &lt;code&gt;settings.py&lt;/code&gt; file we potentially aren't safe yet. If you have made any Git commits before doing this change, our secret key is stored in our Git history. Anyone who can access out source code and Git history can see it. &lt;/p&gt;

&lt;p&gt;The solution is to create a new SECRET_KEY and add it to our &lt;code&gt;.env&lt;/code&gt; file. First ensure that you have created a &lt;code&gt;.gitignore&lt;/code&gt; file in the root directory of your project and add a &lt;code&gt;.env&lt;/code&gt; entry to it so that Git will not track it.&lt;/p&gt;

&lt;p&gt;In order to generate a new SECRET_KEY is by invoking Python's &lt;a href="https://docs.python.org/3/library/secrets.html"&gt;built-in secrets&lt;/a&gt; module by running the following from your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -c "import secrets; print(secrets.token_urlsafe())"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now paste this new randomly generated secret into your &lt;code&gt;.env&lt;/code&gt; file overwriting the &lt;code&gt;SECRET_KEY&lt;/code&gt; entry. Ensure that there are no quotes (" or ') surrounding your pasted key and that there are no spaces between the equals sign and your key in the &lt;code&gt;.env&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;Now you have a more secure secret key! &lt;/p&gt;

</description>
      <category>django</category>
      <category>beginners</category>
      <category>security</category>
      <category>python</category>
    </item>
    <item>
      <title>Django environment variables</title>
      <dc:creator>Rob McBryde</dc:creator>
      <pubDate>Wed, 03 Jan 2024 13:09:50 +0000</pubDate>
      <link>https://dev.to/rob212/django-environment-variables-f2n</link>
      <guid>https://dev.to/rob212/django-environment-variables-f2n</guid>
      <description>&lt;p&gt;As per the &lt;a href="https://12factor.net/"&gt;twelve-factor app methodology&lt;/a&gt; it is important to store config outwith the program that can be injected in at runtime. &lt;/p&gt;

&lt;p&gt;Not only does this keep sensitive information secure such as our passwords and API keys. Additionally it allows us to make us of a single &lt;code&gt;settings.py&lt;/code&gt; file to load either out local or production environments variables.&lt;/p&gt;

&lt;p&gt;There are many options for doing this, but the Django community seems to like &lt;a href="https://github.com/sloria/environs"&gt;environs&lt;/a&gt; as it comes with additional Django support built-in. &lt;/p&gt;

&lt;p&gt;To install it via pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m pip install "environs[django]"==9.5.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then add these three lines to the top of yout &lt;code&gt;project/settings.py&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from environs import Env 

env = Env()
env.read_env()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Database config using environment variables
&lt;/h2&gt;

&lt;p&gt;An example use case is configuring our project to use a SQLite database locally, but a PostgreSQL database in production. &lt;/p&gt;

&lt;p&gt;The environs package comes installed with &lt;a href="https://github.com/jazzband/dj-database-url"&gt;dj-database-url&lt;/a&gt;. A package that allows us to set a &lt;code&gt;DATABASE_URL&lt;/code&gt; that is utilised by most PaaS providers.&lt;/p&gt;

&lt;p&gt;With a very small amount of code in our &lt;code&gt;project/settings.py&lt;/code&gt; file, we can configure our project to try to access a &lt;code&gt;DATABASE_URL&lt;/code&gt; environment variable. If it exists it will be used and our project will access our 'production' PostgreSQL database, if one can't be found then it will default to SQLite, e.g. locally. &lt;/p&gt;

&lt;p&gt;Change the default 'DATABASES' section in your &lt;code&gt;settings.py&lt;/code&gt; file from this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASES = {
    "default": env.dj_db_url("DATABASE_URL", default="sqlite:///db.sqlite3"),
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configuring as PostgreSQL database in Django
&lt;/h2&gt;

&lt;p&gt;In order to use a PostgreSQL database we need an adapter that allows Python to communicate with a PostgreSQL database. The community seems to favour &lt;a href="https://www.psycopg.org/"&gt;Pyscopg&lt;/a&gt; for this. &lt;/p&gt;

&lt;p&gt;If you are on a Mac like me, you will also need to have installed PostgreSQL via homebrew first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install postgresql
python3 -m pip install "psycopg[binary]"==3.1.8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Deploying your first Django App</title>
      <dc:creator>Rob McBryde</dc:creator>
      <pubDate>Tue, 02 Jan 2024 14:12:11 +0000</pubDate>
      <link>https://dev.to/rob212/deploying-your-first-django-app-1mh0</link>
      <guid>https://dev.to/rob212/deploying-your-first-django-app-1mh0</guid>
      <description>&lt;p&gt;When you create a Django project using the &lt;code&gt;startproject&lt;/code&gt; command, settings are default for local development, many of which are not appropriate in production. &lt;/p&gt;

&lt;p&gt;As a simple starter I want to learn how to deploy a Django application into production using a Platform as a Service provider (PaaS). &lt;/p&gt;

&lt;p&gt;For that I will need a &lt;a href="https://www.fullstackpython.com/wsgi-servers.html"&gt;Python WSGI&lt;/a&gt;(Web Server Gateway Interface). I will be using &lt;a href="https://gunicorn.org/"&gt;Gunicorn&lt;/a&gt; for this. &lt;/p&gt;

&lt;h3&gt;
  
  
  Gunicorn
&lt;/h3&gt;

&lt;p&gt;Gunicorn replaces the Django local development server and is installable with pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m pip install gunicorn==20.1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Requirements file
&lt;/h3&gt;

&lt;p&gt;Once installed, in my Django projects virtual environment, I need to ensure that a valid &lt;code&gt;requirements.txt&lt;/code&gt; file exists containing all the python packages needed to run our project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Django project settings
&lt;/h3&gt;

&lt;p&gt;I now need to update the &lt;code&gt;ALLOWED_HOSTS&lt;/code&gt; list in my &lt;code&gt;django-project/settings.py&lt;/code&gt; to represent which host/domain that my site can serve. &lt;/p&gt;

&lt;p&gt;This is a security measure to prevent HTTP Host header attacks. When getting started you can simply use "*" wildcard in order to allow all domains, but make sure you configure this more specifically for your projects going forwards. &lt;/p&gt;

&lt;h3&gt;
  
  
  Docker ignore file
&lt;/h3&gt;

&lt;p&gt;Finally I am going to create a &lt;code&gt;.dockerignore&lt;/code&gt; file in the project root directory. As a minimum, I want to ignore our local virtual environment, SQLite database an Git repo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# .dockerignore file contents
.venv/
*.sqlite3
.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Fly.io
&lt;/h3&gt;

&lt;p&gt;I have previously used Heroku for side project hosting, however as of Nov 2022, Heroku (understandably) no longer offer a free tier. &lt;/p&gt;

&lt;p&gt;I will therefore be using &lt;a href="https://fly.io/"&gt;Fly.io&lt;/a&gt; to host my app. You will need to sign up for an account and a payment method. In my experience, provided you site isn't attracting traffic and costs less than $5 per month, then Fly.io wave the fees and it is free. &lt;/p&gt;

&lt;p&gt;Fly has its own CLI (Command Line Interface) which you can install following the &lt;a href="https://fly.io/docs/hands-on/install-flyctl/"&gt;official docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once installed we login:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flyctl auth login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I then run the &lt;code&gt;fly launch&lt;/code&gt; command to configure and launch my site:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fly launch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Provided you do this in the terminal within your virtual environment at the root of your project, Fly should automatically detect you are running a Django app. Make sure you don't overwrite the &lt;code&gt;.dockerignore&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;Once complete you should see two new files in your project directory. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fly.toml&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Dockerfile&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;fly.toml&lt;/code&gt; is a Fly-specific config file and the Dockerfile contains instructions for creating a Docker image on Fly servers. &lt;/p&gt;

&lt;h4&gt;
  
  
  deploying on Fly
&lt;/h4&gt;

&lt;p&gt;To deploy our app onto Fly servers run the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flyctl deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The initial deploy will take a few seconds as it comprises  of uploading your application, verifies the app configuration, builds the Docker image and then monitors to ensure the app starts successfully. &lt;/p&gt;

&lt;p&gt;Once complete, you can visit your app in production with &lt;code&gt;fly open&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fly apps open
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have any issues you can view the logs via &lt;code&gt;fly logs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I'm not quite sure why some Fly commands require &lt;code&gt;flyctl&lt;/code&gt; while others require &lt;code&gt;fly&lt;/code&gt;, if in doubt check their docs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary of deployment
&lt;/h3&gt;

&lt;p&gt;Our Django site is running on a Docker virtual machine created via the &lt;code&gt;Dockerfile&lt;/code&gt; in our project. The &lt;code&gt;fly.toml&lt;/code&gt; configures our website settings and can be modified as needed. &lt;/p&gt;

&lt;p&gt;You can run &lt;code&gt;fly dashboard&lt;/code&gt; via the command line to load uour browser-based view to manage your Fly deployments. &lt;/p&gt;

&lt;h2&gt;
  
  
  Caveats
&lt;/h2&gt;

&lt;p&gt;I have taken several security shortcuts here as my goal is purely to push my first project into production in as few steps as possible. This steps are not advisable for real production systems that will be used in anger.&lt;/p&gt;

&lt;p&gt;I intend to document proper security around deployments in the future as my learning continues. &lt;/p&gt;

&lt;p&gt;I also haven't compiled static files into one location for production. That too is something I will explore as I continue to learn. This really was about deploying the most minimal app to production.&lt;/p&gt;

</description>
      <category>django</category>
      <category>beginners</category>
      <category>python</category>
      <category>fly</category>
    </item>
    <item>
      <title>Django tests</title>
      <dc:creator>Rob McBryde</dc:creator>
      <pubDate>Tue, 02 Jan 2024 11:56:40 +0000</pubDate>
      <link>https://dev.to/rob212/django-tests-5af1</link>
      <guid>https://dev.to/rob212/django-tests-5af1</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Code without tests is broken as designed&lt;/strong&gt; - Jacob Kaplan-Moss (Django co-creator)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I am a fan of TDD (test driven development) and get nervous anytime I am working with a codebase that does not have a robust automated testing framework. &lt;/p&gt;

&lt;p&gt;Therefore, it's important as part of my learning Django to try out the testing framework to see how it feels to work with. &lt;/p&gt;

&lt;p&gt;According to the &lt;a href="https://docs.djangoproject.com/en/5.0/topics/testing/overview/"&gt;Django official docs&lt;/a&gt; the preferred testing approach is using the &lt;code&gt;unittest&lt;/code&gt; module that is built-in to the Python standard library. &lt;/p&gt;

&lt;p&gt;Django's testing framework provides several extensions on top of the standard &lt;code&gt;unittest.TestCase&lt;/code&gt; base class. These include a test client for making dummy Web browser requests and four test case classes: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;SimpleTestCase&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;TestCase&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;&lt;code&gt;TransactionTestCase&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;LiveServerTestCase&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An overly simplified summary of their use cases is &lt;code&gt;SimpleTestCase&lt;/code&gt; is used when a database is unnecessary. While &lt;code&gt;TestCase&lt;/code&gt; is used when you want to test the database. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;TransactionTestCase&lt;/code&gt; is helpful to directly test databases transactions while &lt;code&gt;LiveServerTestCase&lt;/code&gt; launches a live server thread for testing with browser-based tools such as Selenium. &lt;/p&gt;

&lt;p&gt;One strange convention I found was the fact the &lt;code&gt;unittest&lt;/code&gt; and &lt;code&gt;django.test&lt;/code&gt; methods are written in camelCase rather than the standard snake_case pattern normally found in Python. Apparently, this stems for the fact that &lt;code&gt;unittest&lt;/code&gt; is based on the &lt;code&gt;jUnit&lt;/code&gt; testing framework from Java which utilises camelCase. This seems a bit bizarre to me, but I will stick with the communities conventions. &lt;/p&gt;

&lt;h2&gt;
  
  
  Testing with a database
&lt;/h2&gt;

&lt;p&gt;When testing with a database, we need to use &lt;code&gt;TestCase&lt;/code&gt;, which will let us create a test database. In other words, we don't need to run tests on our &lt;em&gt;actual&lt;/em&gt; database. Instead we can make a separate test database, fill it with sample data pertinent to our scenarios, then test against it. &lt;/p&gt;

&lt;p&gt;This is much safer and a more performant approach to testing against our actual &lt;em&gt;production&lt;/em&gt; database. &lt;/p&gt;

&lt;p&gt;To make use of this, we use the hook &lt;code&gt;setUpTestData()&lt;/code&gt; to create our test data; it is much faster thatn using the &lt;code&gt;setUp()&lt;/code&gt; hook from Python's &lt;code&gt;unittest&lt;/code&gt; because it creates the test data only once per test case rather than per test. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: test functions must start with the word "test" to be run. &lt;/p&gt;

</description>
      <category>django</category>
      <category>testing</category>
      <category>beginners</category>
      <category>python</category>
    </item>
    <item>
      <title>Django Views</title>
      <dc:creator>Rob McBryde</dc:creator>
      <pubDate>Tue, 02 Jan 2024 11:22:51 +0000</pubDate>
      <link>https://dev.to/rob212/django-views-3hgo</link>
      <guid>https://dev.to/rob212/django-views-3hgo</guid>
      <description>&lt;p&gt;As of the day I write this post, Django supports 3 ways to create a view: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;function-based&lt;/li&gt;
&lt;li&gt;class-based&lt;/li&gt;
&lt;li&gt;generic class-based&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most problems you are trying to solve have probably already been done via a generic class based view. If it hasn't it is often possible to modify one of the generic views to suit your needs. &lt;/p&gt;

&lt;p&gt;This will be my default approach to always utilise a generic class based view. Failing that I will fall back to creating a class-based view, then a function-based view. &lt;/p&gt;

&lt;p&gt;For further learnings on this topic, check out the very useful &lt;a href="https://ccbv.co.uk/"&gt;Classy Class-Based Views site&lt;/a&gt;. &lt;/p&gt;

</description>
      <category>django</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Embarking on a Django journey - Coding for the joy of it</title>
      <dc:creator>Rob McBryde</dc:creator>
      <pubDate>Fri, 29 Dec 2023 14:13:29 +0000</pubDate>
      <link>https://dev.to/rob212/embarking-on-a-django-journey-coding-for-the-joy-of-it-37ng</link>
      <guid>https://dev.to/rob212/embarking-on-a-django-journey-coding-for-the-joy-of-it-37ng</guid>
      <description>&lt;p&gt;Django setup on Mac with venv, PyCharm and Black. &lt;/p&gt;

&lt;p&gt;As a software engineering manager entrenched in the world of people management and administrative tasks, the yearning to reconnect with the sheer joy of coding has led me to explore a new and exciting realm: the &lt;a href="https://www.djangoproject.com/"&gt;Django framework in Python&lt;/a&gt;. In this blog post, I'll share the motivations behind this creative coding endeavour and delve into the initial steps of setting up a development environment on my Mac.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Creative Coding Escape
&lt;/h3&gt;

&lt;p&gt;Amidst the hustle of managerial responsibilities, I found myself yearning for a coding outlet that could both rekindle the joy of programming and introduce me to unfamiliar territories. Django, a high-level web framework written in Python, presented itself as an intriguing choice. The decision to delve into a language I have limited professional experience with and a framework entirely unexplored felt perfect for me. &lt;/p&gt;

&lt;h3&gt;
  
  
  Setting the Stage: Python and Django on Mac
&lt;/h3&gt;

&lt;p&gt;The first step on this journey involved setting up a development environment tailored for Python and Django on my Mac. &lt;/p&gt;

&lt;h4&gt;
  
  
  Pyenv
&lt;/h4&gt;

&lt;p&gt;In my experience it is always advisable to install a version manager when working with programmgin languages where possible. Your future self will help you for it if you start to work on multiple projects in the future. In order to install Python 3 onto my Mac I used &lt;a href="https://github.com/pyenv/pyenv"&gt;pyenv&lt;/a&gt; which I installed via &lt;a href="https://brew.sh/"&gt;Homebrew&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Python virtual environments
&lt;/h3&gt;

&lt;p&gt;Consider working on multiple Django projects at once. Project A uses Django 3.2, but Project B uses Django 4.2. By default, Python and Django are installed &lt;em&gt;globally&lt;/em&gt; on a computer: installing and reinstalling different versions every time you want to switch between project, quickly becomes a real pain. &lt;/p&gt;

&lt;p&gt;Virtual environments allow you to create and manage separate environments for each Python project on the same computer. Within the Python community, the use of virtual environments seems non debatable. I have opted to use &lt;em&gt;venv&lt;/em&gt; which comes installed as part of Python version 3.3, so fits my needs perfectly.  &lt;/p&gt;

&lt;p&gt;I am using the convention of creating a &lt;em&gt;.venv&lt;/em&gt; directory alongside each of my Django projects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir helloworld
cd helloworld
python3 -m venv .venv
# active the virtual environment
source .venv/bin/activate
python3 -m pip install django~=4.2.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command to install Django in my virtual environment uses the comparison operator &lt;em&gt;~=&lt;/em&gt;, which installs the latest version of Django 4.2.x. &lt;/p&gt;

&lt;p&gt;I didn't use &lt;em&gt;pip install&lt;/em&gt; but rather &lt;em&gt;python3 -m pip install&lt;/em&gt;? The former does work, but according to my reading around the Python community, it can cause some issues. Using the latter version of the comman ensures that the intended vesion of Python is in use, even if you have multiple versions of Python installed in your machine. Given that Macs often have Python 2.7 installed, it seems like a sensible approach to me to ensure the intended version of Python is being used. &lt;/p&gt;

&lt;h3&gt;
  
  
  Crafting Code in Comfort: PyCharm IDE
&lt;/h3&gt;

&lt;p&gt;Choosing the right Integrated Development Environment (IDE) is crucial for a seamless coding experience. Enter &lt;a href="https://www.jetbrains.com/pycharm/"&gt;PyCharm&lt;/a&gt; – my preferred IDE for this Django expedition. Its robust features, intelligent code completion, and Django support make it an invaluable companion for navigating the intricacies of web development. The comfort of PyCharm enhances the joy of coding, allowing me to focus on creativity rather than grappling with the tool. I'm a long term fan of JetBrains, so sticking with what I know. &lt;/p&gt;

&lt;h3&gt;
  
  
  Code Formatting Bliss with Black
&lt;/h3&gt;

&lt;p&gt;No coding journey is complete without attention to code formatting. Enter "&lt;a href="https://pypi.org/project/black/"&gt;Black&lt;/a&gt;," the uncompromising code formatter for Python. Its opinionated approach streamlines the process, ensuring consistent and readable code. Integrating Black into the workflow not only adheres to best practices but also eliminates unnecessary debates over code styling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m pip install black 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As of PyCharm 2023.2, Black integration has been possible via Settings &amp;gt; Tools &amp;gt; Black. Simply select your Python interpreter as your (helloworld) virtual env. FYI I'm currently using Python 3.10 but will no doubt change soon enough (easily due to pyenv). I happen to like the Black formatter running 'On save'&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;That's my basic setup complete. &lt;/p&gt;

&lt;p&gt;In the coming posts, I'll be documenting my learning experiences, pitfalls, and triumphs in the Django universe. While the primary aim is personal growth and enjoyment, I hope this documentation serves as a valuable resource for others venturing into Django development. Here's to coding for the sheer thrill of it!&lt;/p&gt;

</description>
      <category>django</category>
      <category>venv</category>
      <category>pycharm</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
