0% found this document useful (0 votes)
5 views

Django Project Creation Guide (3)

This document provides a step-by-step guide for creating a Django project, including setting up a folder, creating a virtual environment, installing dependencies, and running the project. It also covers deployment on Google App Engine and setting up Git for version control. The instructions are detailed for both Windows and Linux/Mac users, ensuring a comprehensive approach to Django project management.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Django Project Creation Guide (3)

This document provides a step-by-step guide for creating a Django project, including setting up a folder, creating a virtual environment, installing dependencies, and running the project. It also covers deployment on Google App Engine and setting up Git for version control. The instructions are detailed for both Windows and Linux/Mac users, ensuring a comprehensive approach to Django project management.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Django Project Creation Guide

Step 1: Create a Folder for the Project

Before starting, create a folder to keep all project files organized. You can
name it anything you like. For example:

●​ On Windows (Command Prompt or PowerShell):​


mkdir my_django_project

●​ On Linux/Mac (Terminal):​
mkdir my_django_project

Step 2: Navigate to the Folder

Move into the newly created directory:

●​ cd my_django_project

Step 3: Create a Django Project

Use the Django command-line tool to create a new project:

●​ django-admin startproject yaapiyal_django

This will create a folder named yaapiyal_django containing essential


Django files.

Step 4: Navigate into the Django Project


●​ cd yaapiyal_django
Step 5: Create a Django App

Django projects are composed of apps. Create an app within the project:

●​ django-admin startapp yaapiyal

This will generate a new app named yaapiyal within the project.

Step 6: Create a Virtual Environment

A virtual environment keeps your project dependencies isolated.

●​ python -m venv venv

Step 7: Activate the Virtual Environment


●​ Windows (Command Prompt):​
venv\Scripts\activate
●​ Windows (PowerShell):​
.\venv\Scripts\Activate.ps1
●​ Mac/Linux:​
source venv/bin/activate

Virtual Environment Documentation

Step 8: Install Django

Ensure Django is installed in your virtual environment:

●​ pip install django

Step 9: Install MySQL Dependencies

If using MySQL, install the necessary package:


●​ pip install mysqlclient

Step 10: Install Dependencies from requirements.txt

If a requirements.txt file exists, install dependencies with:

●​ pip install -r requirements.txt

Step 11: Apply Migrations to the Database

Django uses migrations to structure the database:

●​ python manage.py makemigrations


●​ python manage.py migrate

Step 12: Create a Superuser

To manage the Django admin panel, create an admin account:

●​ python manage.py createsuperuser

Follow the prompts to enter a username, email, and password.

Step 13: Run the Django Project

Start the Django development server:

●​ python manage.py runserver

Visit http://127.0.0.1:8000/ in a web browser to see your project


running.
Deployment on Google App Engine (GAE)
Step 1: Create a requirements.txt File

Generate a file listing all dependencies:

●​ pip freeze > requirements.txt

Step 2: Create a main.py File

This file ensures Django runs properly on Google App Engine. Create
main.py and add the following:

●​ from yaapiyal_django.wsgi import application


●​ app = application

Where the yaapiyal_django in yaapiyal_django.wsgi is the Name of


the Django Project

Step 3: Create an app.yaml File

This file tells Google App Engine how to run the project. Create app.yaml
and add:

●​ runtime: python39

Step 4: Update Allowed Hosts in settings.py

Modify settings.py to allow requests from the cloud:

●​ ALLOWED_HOSTS = ['your_project_url', '127.0.0.1']

Step 5: Initialize Google Cloud Project


●​ gcloud init
Follow the instructions to set up Google Cloud.

Step 6: Authenticate Your Google Account


●​ gcloud auth login

Step 7: Set the Google Cloud Project


●​ gcloud config set project YOUR_PROJECT_ID

Step 8: Deploy the Application


●​ gcloud app deploy

Step 9: Access the Deployed Application


●​ gcloud app browse

Google Cloud Proxy Command for Windows PowerShell


●​ .\cloud-sql-proxy.exe django-demo-322916:us-central1:djangodemo

Git Setup for Django Project


Step 1: Install Git

Check if Git is installed:

●​ git --version

If not installed:

●​ Ubuntu/Debian:​
sudo apt update && sudo apt install git
●​ Mac:​
brew install git
●​ Windows: Download from git-scm.com

Step 2: Navigate to the Project Directory

Move into your Django project folder:

●​ cd /path/to/your/django/app

Step 3: Initialize a Git Repository


●​ git init

Step 4: Create a .gitignore File

To prevent tracking unnecessary files, create .gitignore:

●​ touch .gitignore

Add the following:

# Python
*.pyc
*.pyo
__pycache__/

# Environment
.env
*.env
# Logs
*.log

# IDEs/Editors
.vscode/
.idea/
# Others
.DS_Store

# Django
/static/
/media/

Step 5: Stage and Commit Files


●​ git add .
●​ git commit -m "Initial commit for Django app"

Step 6: Connect to a Remote Repository (Optional)

Create a repository on GitHub or GitLab, then link it:

●​ git remote add origin https://github.com/your-username/your-repo.git


●​ git branch -M main
●​ git push -u origin main

Step 7: Use Version Control Regularly


●​ Check status:​
git status
●​ Add changes:​
git add .
●​ Commit changes:​
git commit -m "Your commit message"
●​ Push to remote:​
git push

This setup ensures your Django project is properly version-controlled and


can be deployed easily.

You might also like