<?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: pratyush</title>
    <description>The latest articles on DEV Community by pratyush (@pratyushcode).</description>
    <link>https://dev.to/pratyushcode</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%2F717401%2F74602383-873d-46ab-985b-54f713dfde1c.png</url>
      <title>DEV Community: pratyush</title>
      <link>https://dev.to/pratyushcode</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pratyushcode"/>
    <language>en</language>
    <item>
      <title>Mastering Django: Best Practices for Writing High-Quality Web Applications</title>
      <dc:creator>pratyush</dc:creator>
      <pubDate>Wed, 05 Apr 2023 10:10:29 +0000</pubDate>
      <link>https://dev.to/pratyushcode/mastering-django-best-practices-for-writing-high-quality-web-applications-2mf8</link>
      <guid>https://dev.to/pratyushcode/mastering-django-best-practices-for-writing-high-quality-web-applications-2mf8</guid>
      <description>&lt;p&gt;&lt;a href="https://docs.djangoproject.com/en/4.2/" rel="noopener noreferrer"&gt;Django&lt;/a&gt; is a popular web framework for building high-quality web applications. It provides a robust set of tools and features that enable developers to build web applications quickly and efficiently. &lt;/p&gt;

&lt;p&gt;However, building a high-quality web application requires more than just knowing the basics of Django. To build a robust, scalable, and maintainable application, developers need to follow best practices and use Django's features correctly. &lt;/p&gt;

&lt;p&gt;In this blog post, we'll explore four essential practices that will help you keep your project organized, secure, and production-ready. &lt;/p&gt;

&lt;p&gt;We'll cover how to create and update a &lt;code&gt;.gitignore&lt;/code&gt; file, how to manage sensitive information using &lt;code&gt;.env&lt;/code&gt; files, how to use the Django settings package to manage project settings, and finally, we'll share some standard practices to keep your project production-ready. Let's get started!&lt;/p&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt;  To highlight these design and configuration changes, I have authored a repo &lt;code&gt;pratyzsh/django-best-practices&lt;/code&gt;. Please feel free to raise a pull request and support the project&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/beeryani" rel="noopener noreferrer"&gt;
        beeryani
      &lt;/a&gt; / &lt;a href="https://github.com/beeryani/django-best-practices" rel="noopener noreferrer"&gt;
        django-best-practices
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Best practices every Django developer should follow from start to finish
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Django Best Practices&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;This repository provides a collection of best practices for developing Django web applications. These practices are based on industry standards and can help you write more robust, maintainable, and scalable Django code.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Table of Contents&lt;/h2&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/beeryani/django-best-practices#getting-started" rel="noopener noreferrer"&gt;Getting Started&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/beeryani/django-best-practices#project-structure" rel="noopener noreferrer"&gt;Project Structure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/beeryani/django-best-practices#environment-configuration" rel="noopener noreferrer"&gt;Environment Configuration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/beeryani/django-best-practices#postgresql-database-connection" rel="noopener noreferrer"&gt;PostgreSQL Database Connection&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/beeryani/django-best-practices#security" rel="noopener noreferrer"&gt;Security&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/beeryani/django-best-practices#testing" rel="noopener noreferrer"&gt;Testing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/beeryani/django-best-practices#deployment" rel="noopener noreferrer"&gt;Deployment&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/beeryani/django-best-practices#contributing" rel="noopener noreferrer"&gt;Contributing&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/beeryani/django-best-practices#license" rel="noopener noreferrer"&gt;License&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Getting Started&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;First clone the repository from Github and switch to the new directory:&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;$ git clone https://github.com/pratyzsh/django-best-practices
$ cd django-best-practices
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Activate the virtualenv for your project.&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;$ source django_env/bin/activate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Install project dependencies:&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;$ pip install -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Then simply apply the migrations:&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;$ python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can now run the development server:&lt;/p&gt;
&lt;div class="snippet-clipboard-content notranslate position-relative overflow-auto"&gt;&lt;pre class="notranslate"&gt;&lt;code&gt;$ python manage.py runserver
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Project Structure&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;A well-organized project structure is key to writing maintainable code. This section provides recommendations for organizing your Django project.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Environment Configuration&lt;/h2&gt;

&lt;/div&gt;
&lt;p&gt;Managing environment variables is important for keeping your application secure and flexible. This section covers how to…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/beeryani/django-best-practices" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;





&lt;h3&gt;
  
  
  1. Create and update &lt;code&gt;.gitignore&lt;/code&gt; file
&lt;/h3&gt;

&lt;p&gt;We need .gitignore files to exclude certain files and directories from being tracked by Git. This helps to keep our repositories clean and avoids cluttering them with unnecessary files. Additionally, it improves repository performance and security by preventing sensitive or large files from being accidentally committed.&lt;/p&gt;

&lt;p&gt;Some common files you should always aim to exclude from commit history using &lt;code&gt;.gitignore&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Virtual Environment folder&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.env&lt;/code&gt; files&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;static/&lt;/code&gt; folder&lt;/li&gt;
&lt;li&gt;Database files, in Django, we have &lt;code&gt;db.sqlite3&lt;/code&gt; database as default which needs to be excluded&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For Django, you can use this &lt;a href="https://github.com/jpadilla/django-project-template/blob/master/.gitignore" rel="noopener noreferrer"&gt;base template&lt;/a&gt; and extend it based on your project requirements.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Add &lt;code&gt;.env&lt;/code&gt; files to manage sensitive information
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;.env&lt;/code&gt; files are used to securely store sensitive information such as passwords and API keys. They keep this information separate from source code and configuration files. Additionally, they allow for easy switching between different environments without modifying code.&lt;/p&gt;

&lt;p&gt;In case of Django, we have a bunch of sensitive information to deal with, like &lt;code&gt;SECRET_KEY&lt;/code&gt;, &lt;code&gt;DATABASE_NAME&lt;/code&gt;, &lt;code&gt;DATABASE_PASS&lt;/code&gt; and so on.&lt;/p&gt;

&lt;p&gt;Here's an example how we can do this in Django.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create &lt;code&gt;.env&lt;/code&gt; file in the same directory as your &lt;code&gt;settings.py&lt;/code&gt; file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;.env&lt;/code&gt; file should look like this:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE_NAME=postgres
DATABASE_USER=adam
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;After storing the secrets in the above file, we can configure the &lt;code&gt;settings.py&lt;/code&gt; file like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import environ

env = environ.Env()
# reading .env file
environ.Env.read_env()

SECRET_KEY = env("SECRET_KEY")

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': env("DATABASE_NAME"),
        'USER': env("DATABASE_USER"),
        'PASSWORD': env("DATABASE_PASSWORD"),
        'HOST': env("DATABASE_HOST"),
        'PORT': env("DATABASE_PORT"),
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Refer to the &lt;a href="https://github.com/pratyzsh/django-best-practices" rel="noopener noreferrer"&gt;repository&lt;/a&gt; to understand the implementation details.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Create different &lt;code&gt;settings.py&lt;/code&gt; for different deployment environments
&lt;/h3&gt;

&lt;p&gt;When you're developing a Django project, you'll typically have different settings depending on the environment you're working in, such as development, testing, staging, and production.&lt;/p&gt;

&lt;p&gt;Few key reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Security: Production environments have different security requirements than development or testing environments. Using different settings files ensures that security measures like HTTPS and limited database permissions are properly implemented.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performance: Production environments have different performance requirements than development or testing environments. Using different settings files ensures that performance measures like caching and load balancing are properly implemented.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Debugging: Debugging in production is different than debugging in development or testing. Different logging or error reporting tools might be needed, which can be specified in different settings files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Customisation: Different environments might have different requirements or configurations that need to be customised, such as email or SMS providers. Using different settings files ensures that these Customisation are properly implemented for each environment.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To manage this, there are multiple approaches that you can take. I learnt a lot from &lt;a href="https://djangostars.com/blog/configuring-django-settings-best-practices/" rel="noopener noreferrer"&gt;this article&lt;/a&gt; and the approach I borrowed is creating a separate &lt;code&gt;settings/&lt;/code&gt; package and change the reference in &lt;code&gt;manage.py&lt;/code&gt; file.&lt;br&gt;
The final project structure should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\
+-- sampleproject
|   +-- settings.py #default settings file
|   +-- ...
+-- settings #package with base.py as the new default settings file
|   +-- __init__.py
|   +-- base.py 
|   +-- development.py
|   +-- production.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have created a separate folder and added your separate setting files, make changes in the &lt;code&gt;manage.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;...
def main():
    """Run administrative tasks."""
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.base") 
'''
reference changed to settings/base.py in the above line
'''
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have made these changes, you can independently manage all environments by changing their respecting settings.&lt;/p&gt;

&lt;p&gt;In order to run the application using &lt;code&gt;development.py&lt;/code&gt; in the example mentioned above, either we can change the &lt;code&gt;manage.py&lt;/code&gt; based on our project requirement but you also run this command to extend settings dynamically.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py runserver --settings=settings.development
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a fast way to toggle between environments before initiating the deployment process.&lt;/p&gt;

&lt;p&gt;Refer to the &lt;a href="https://github.com/pratyzsh/django-best-practices" rel="noopener noreferrer"&gt;repository&lt;/a&gt; to understand the implementation details.&lt;/p&gt;




&lt;p&gt;As a final note, one thing I have personally found to be very useful in ensuring these best practices are followed, is to ensure proper naming, formatting and linting of my code. &lt;/p&gt;

&lt;p&gt;You can check out my other blog, &lt;a href="https://dev.to/pratyushcode/streamlining-your-python-workflow-with-black-ruff-github-actions-and-pre-commit-hooks-nk2"&gt;Streamlining Your Python Workflow with Black, Ruff, GitHub Actions, and Pre-Commit Hooks&lt;/a&gt; which is also used in the repository. Do check out the blog and repository. Like and share if you find this valuable.&lt;/p&gt;

&lt;p&gt;Implement these and experience a blazing fast workflow ✨✨✨&lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>productivity</category>
      <category>testing</category>
    </item>
    <item>
      <title>Streamlining Your Python Workflow with Black, Ruff, GitHub Actions, and Pre-Commit Hooks</title>
      <dc:creator>pratyush</dc:creator>
      <pubDate>Sun, 26 Mar 2023 08:34:27 +0000</pubDate>
      <link>https://dev.to/pratyushcode/streamlining-your-python-workflow-with-black-ruff-github-actions-and-pre-commit-hooks-nk2</link>
      <guid>https://dev.to/pratyushcode/streamlining-your-python-workflow-with-black-ruff-github-actions-and-pre-commit-hooks-nk2</guid>
      <description>&lt;p&gt;In today's fast-paced software development world, maintaining code quality and consistency is of utmost importance. Python, being one of the most popular programming languages, has several tools to help developers achieve this goal. Black, a Python code formatter, and Ruff, a Python linter, are two such tools that can help ensure that your Python code is formatted correctly and follows best practices. &lt;/p&gt;

&lt;p&gt;In this dev blog, we'll explore how to use Black and Ruff to automatically format and lint your Python code, and integrate them seamlessly into your workflow using GitHub Actions and pre-commit hooks. By the end of this blog, you'll have a better understanding of how to use these tools to improve the quality of your Python code and streamline your development process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Black ✨
&lt;/h2&gt;

&lt;p&gt;Black is an open-source Python code formatter that is designed to make your code more readable and maintainable by enforcing a consistent style. It is a fast and efficient tool that automatically reformats your code to adhere to the official Python style guide, PEP 8. Check out the repo &lt;a href="https://github.com/psf/black#installation-and-usage"&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Ruff ⚡
&lt;/h2&gt;

&lt;p&gt;Ruff is a Python linter and code formatter tool. It is designed to help developers maintain consistent and clean Python code by providing automated code analysis and formatting. Some of its key features include support for various Python versions, integration with popular IDEs and editors, and customisable configuration options. Check out the well written documentation &lt;a href="https://beta.ruff.rs/docs/installation/"&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Integrating the tools in workflow
&lt;/h2&gt;

&lt;p&gt;To keep your code in top notch shape, ready for production deployment with minimal requirements, it is important to have these checks present at all touch points of the software development lifecycle.&lt;/p&gt;

&lt;p&gt;Pre-commit Hooks and Github Actions come into picture to ensure just that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-commit hooks
&lt;/h2&gt;

&lt;p&gt;Pre-commit hooks are automated checks that run before code changes are committed. They enforce code style consistency and prevent issues, such as poorly formatted code, through checks like formatting, linting, unit testing, and security scanning. Pre-commit hooks catch problems early and prevent them from causing larger issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Github Actions
&lt;/h2&gt;

&lt;p&gt;GitHub Actions is a built-in automation tool in GitHub that allows developers to automate tasks like building, testing, and deploying code. It has a flexible YAML-based syntax, integrates with various tools, and offers pre-built actions in a marketplace.&lt;/p&gt;




&lt;p&gt;Pre-commit hooks and Github Actions work in sync with each other. One ensures the code committed to version control meets defined standards, the other ensures those high standards are maintained in collaborative settings.&lt;/p&gt;

&lt;p&gt;Now, we will see how to install, configure and implement these tools to achieve our productivity goals.&lt;/p&gt;

&lt;h2&gt;
  
  
  Workflow
&lt;/h2&gt;

&lt;p&gt;To better understand the implementation details, we will refer to a live example, which relies on Black for code formatting, on Ruff for linting, pre-commit hooks to ensure only PEP8 grade code is committed and Github Actions to ensure these standards across the code lifecycle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install git+https://github.com/psf/black
pip install ruff
pip install pre-commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring pre-commit hooks
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt; file at the root directory of your project folder.&lt;/li&gt;
&lt;li&gt;Initialise the following sample hooks:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.3.0
    hooks:
      - id: check-yaml
      - id: end-of-file-fixer
      - id: trailing-whitespace
  - repo: https://github.com/psf/black
    rev: 22.10.0
    hooks:
      - id: black
  - repo: https://github.com/charliermarsh/ruff-pre-commit
    # Ruff version.
    rev: "v0.0.257"
    hooks:
      - id: ruff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Install the hooks using &lt;code&gt;pre-commit install&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Do a test run using &lt;code&gt;pre-commit run --all-files&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configuring Github Actions
&lt;/h3&gt;

&lt;p&gt;We don't have dedicated testing actions for Black and Ruff so we will write the actions in the standard &lt;code&gt;.github/workflows&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Python Linting and Formatting

on: [push, pull_request]

jobs:
  lint:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: "3.x"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Lint with Ruff
        run: ruff .

      - name: Format with Black
        run: black .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just commit your changes and experience the magic of automated testing ✨✨✨&lt;/p&gt;

</description>
      <category>githubactions</category>
      <category>python</category>
      <category>productivity</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
