Home > Backend Development > Python Tutorial > Bulletproof Django API for a TMS project

Bulletproof Django API for a TMS project

Patricia Arquette
Release: 2025-01-19 22:14:12
Original
587 people have browsed it

Bulletproof Django API for a TMS project

Introduction

This tutorial guides you through building a basic web application using Django and Django REST Framework (DRF). The application will manage a book collection, enabling CRUD (Create, Read, Update, Delete) operations via a REST API.

Prerequisites

  • Python 3.8 or higher.
  • Foundational knowledge of Python and Django.
  • pip (Python package installer).
  • Familiarity with virtual environment setup (e.g., venv).

Step 1: Environment Setup

Recommended VM Configuration:

  • RAM: 8 GB
  • Processors: 4 cores (8 threads)
  • Storage: 100 GB
  • Network: Bridged Networking (for a unique IP) or NAT (for internet access via the host).

VM Setup:

  1. Download the ubuntu-22.04-server-cloudimg-amd64.ova image from a reliable source (like the official Ubuntu site).
  2. Open your VMware Workstation, Fusion, or ESXi and import the OVA file. Follow the on-screen instructions.
  3. Name your VM (e.g., TMS_VM).
  4. Configure the VM's memory, processors, hard disk, and network settings according to the recommendations above.

Ubuntu 22.04 Server Configuration:

  1. Start the VM.

  2. Log in and update the system: sudo apt update && sudo apt upgrade -y

  3. Install essential tools: sudo apt install git python3 python3.10-venv python3-pip python3-venv git build-essential -y

  4. Create a user: The following commands create a user named django with appropriate permissions. Remember to replace "your_email@example.com" with your actual email address.

    <code class="language-bash">sudo groupadd bulletproof
    sudo adduser django
    sudo usermod -aG bulletproof django</code>
    Copy after login
  5. Create a project directory: mkdir /home/django/projects

  6. Adjust group ownership and permissions:

    <code class="language-bash">sudo chown :bulletproof /home/django/projects
    sudo chmod 775 /home/django/projects
    sudo usermod -d /home/django/projects django
    sudo chown django:bulletproof /home/django/projects
    su - django</code>
    Copy after login

VS Code Setup (Remote-SSH):

Install the Python, Pylance, Flake8, Black, and Django extensions in VS Code. Ensure Flake8 and Black are also installed on the VM using pip. Configure VS Code to use the virtual environment's Python interpreter and enable linting and formatting. Create pyproject.toml and .flake8 files for configuration (see examples in the original document).

Python Setup:

  1. Create a virtual environment:

    <code class="language-bash">mkdir tms && cd tms
    python3 -m venv .venv
    source .venv/bin/activate  # (env\Scripts\activate on Windows)</code>
    Copy after login
  2. Install Django and DRF: pip install django djangorestframework

  3. Create a Django project: django-admin startproject tms .

  4. Run the development server: python manage.py runserver 0.0.0.0:8000

    If you encounter a DisallowedHost error, add your server's IP address to ALLOWED_HOSTS in settings.py.

Git Repository Setup:

  1. Create README.md, requirements.txt (using pip freeze > requirements.txt), LICENSE, and .gitignore files.
  2. Generate an SSH key: ssh-keygen -t ed25519 -C "your_email@example.com". Add the public key to your GitHub account.
  3. Initialize the Git repository: git init
  4. Add a remote repository: git remote add origin git@github.com:username/repository.git
  5. Commit and push your changes: git add ., git commit -m "Initial commit", git push -u origin main

VM Snapshot:

Create a snapshot of your VM after the initial setup using VMware's snapshot functionality. Name it something descriptive, like "InitialSetup".

The above is the detailed content of Bulletproof Django API for a TMS project. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template