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

Django_Project_Setup_Guide

This guide provides a step-by-step process for setting up a Django project, including installing Python and Django, creating a virtual environment, and configuring a database. It covers creating a Django project and app, registering the app, running migrations, and setting up URL routes and views. Additionally, it includes optional steps for installing additional packages, setting up Git, and deploying the project.

Uploaded by

KingStar Oti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Django_Project_Setup_Guide

This guide provides a step-by-step process for setting up a Django project, including installing Python and Django, creating a virtual environment, and configuring a database. It covers creating a Django project and app, registering the app, running migrations, and setting up URL routes and views. Additionally, it includes optional steps for installing additional packages, setting up Git, and deploying the project.

Uploaded by

KingStar Oti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Django Project Setup Guide

1. Install Python and Django

Ensure Python and `pip` are installed on your system. Install Django globally or in a virtual

environment.

# Check Python version

python --version

# Install virtualenv if not already installed

pip install virtualenv

2. Create and Activate a Virtual Environment

# Create a virtual environment

virtualenv venv

# Activate the virtual environment

# On Windows:

venv\Scripts\activate

# On macOS/Linux:

source venv/bin/activate

3. Install Django

pip install django


4. Create a Django Project

# Replace "myproject" with your desired project name

django-admin startproject myproject

cd myproject

5. Create a Django App

# Replace "myapp" with your desired app name

python manage.py startapp myapp

6. Register the App in settings.py

Add the app to INSTALLED_APPS in myproject/settings.py:

INSTALLED_APPS = [

# Default apps...

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

# Your app

'myapp',
]

7. Configure Database

By default, Django uses SQLite. If you want to use another database (e.g., PostgreSQL, MySQL),

update the DATABASES section in myproject/settings.py.

Example for PostgreSQL:

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.postgresql',

'NAME': 'your_db_name',

'USER': 'your_db_user',

'PASSWORD': 'your_password',

'HOST': 'localhost',

'PORT': '5432',

Install the required database adapter:

# For PostgreSQL

pip install psycopg2-binary

# For MySQL

pip install mysqlclient


8. Run Migrations

# Apply initial database migrations

python manage.py migrate

9. Create a Superuser

python manage.py createsuperuser

Follow the prompts to set up admin credentials.

10. Run the Development Server

python manage.py runserver

Visit the development server at http://127.0.0.1:8000.

11. Create URL Routes

Define your app's URL routes in myapp/urls.py and include it in the project's myproject/urls.py.

myapp/urls.py

from django.urls import path

from . import views

urlpatterns = [

path('', views.home, name='home'),

]
myproject/urls.py

from django.contrib import admin

from django.urls import path, include

urlpatterns = [

path('admin/', admin.site.urls),

path('', include('myapp.urls')),

12. Create Views

Define a view in myapp/views.py:

from django.http import HttpResponse

def home(request):

return HttpResponse("Hello, World!")

13. Set Up Static and Media Files

Add the following settings in myproject/settings.py:

STATIC_URL = '/static/'

STATICFILES_DIRS = [BASE_DIR / 'static']

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

Create the static and media folders:

mkdir static media

14. Install Additional Packages (Optional)

For common requirements, install:

- Django REST Framework:

pip install djangorestframework

Add 'rest_framework' to INSTALLED_APPS.

- Django CORS Headers:

pip install django-cors-headers

Add 'corsheaders' to INSTALLED_APPS and middleware.

15. Set Up Git (Optional)

Initialize a Git repository for version control:

git init

echo "venv/" > .gitignore

git add .

git commit -m "Initial commit"

16. Deploy (Optional)

For deployment, configure settings for production and use a platform like:
- Heroku

- AWS Elastic Beanstalk

- Vercel

- Dockerized environments

You might also like