Django


How to Install Django and Create a Project

  1. Ensure Python and pip are installed. If not installed Download Python, or install PyEnv-Win.

    SHELL

    Copied!

    
    python --version
    pip --version
  2. In you project folder activate a virtual environment (recommended) Virtual Environment (venv)

  3. Install Django

    SHELL

    Copied!

    
    pip install django
  4. Can verify installation by checking the current version

    SHELL

    Copied!

    
    django-admin --version
  5. It's a good habit (though optional) to save your installed packages.

    SHELL

    Copied!

    
    pip freeze > requirements.txt
  6. After installing Django you can create a project. Replace 'myproject' with whatever name you like.

    SHELL

    Copied!

    
    django-admin startproject myproject .
  7. Use the cd command to change your current location in the terminal to your project folder. This lets you work inside that folder where your Django project files are. Replace 'myproject' with the name that you used.

    SHELL

    Copied!

    
    cd myproject
  8. Once you are inside the folder containing manage.py (usually the project root), you can start the Django development server.

    SHELL

    Copied!

    
    python manage.py runserver

Creating a new App

  1. This command creates a new Django app named myapp within your project (you can name it whatever you like).

    SHELL

    Copied!

    
    python manage.py startapp myapp
  2. Add the new app to installed apps within the settings.py file

    SHELL

    Copied!

    
    python manage.py startapp myapp

Django Database Management and Migrations

  1. Detect model changes and prepare migration scripts.

    SHELL

    Copied!

    
    python manage.py makemigrations
  2. Use this to generate migration files only for the app you are working on, rather than scanning the entire project.

    SHELL

    Copied!

    
    python manage.py makemigrations myapp
  3. Check what migrations will be created (Optional) useful for debugging.

    SHELL

    Copied!

    
    python manage.py makemigrations --dry-run
  4. Update the database schema based on migration files.

    SHELL

    Copied!

    
    python manage.py migrate
  5. This command runs the database migrations only for the specified app (myapp), instead of applying migrations for all installed apps in the project.

    SHELL

    Copied!

    
    python manage.py migrate myapp
  6. View which migrations have been applied or are pending.

    SHELL

    Copied!

    
    python manage.py showmigrations

Debugging Migrations

  1. This command rolls back all migrations.

    SHELL

    Copied!

    
    python manage.py migrate zero
  2. This command rolls back all migrations for the specified app (myapp), returning it to its initial state.

    SHELL

    Copied!

    
    python manage.py migrate myapp zero
  3. Deletes all data from the database across all apps, while keeping the database schema and applied migrations intact.

    SHELL

    Copied!

    
    python manage.py flush

Create a Superuser

  • Once you've created an app, you can create a superuser. An admin account with full access to the Django admin panel. This account includes a username, email, and password, which you'll use to log in.

    SHELL

    Copied!

    
    python manage.py createsuperuser