How to create a django project?

Step 1: Install Python

brew install python

Step 2: Install Virtual Environment

Virtual environments help manage dependencies and avoid conflicts. To create a virtual environment, you can use venv, which is included with Python

python -m venv myenv

This command creates a directory structure like this:

For MacOS:
myenv/
├── bin/
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── python -> python3
│   ├── python3
│   ├── python3.12
│   └── .....
├── include/
├── lib/
│   └── python3.12/
│       └── site-packages/
├── pyvenv.cfg
For Windows:
myenv/
├── Include
├── Lib
│   ├── site-packages
│   │   ├── __pycache__
│   │   └── ...
│   └── ...
├── Scripts
│   ├── activate
│   ├── activate.bat
│   ├── deactivate.bat
│   ├── pip.exe
│   ├── python.exe
│   ├── pythonw.exe
│   └── ...
└── pyvenv.cfg

Step 3: Activate the Virtual Environment

  • Windows:

      myenv\Scripts\activate
    
  • macOS and Linux:

      source myenv/bin/activate
    

Step 4: Install Django

With the virtual environment activated, install Django using pip.

pip install django

Step 5: Create a Django Project

Use the django-admin command to create a new Django project. Replace myproject with your desired project name.

django-admin startproject myproject

This command creates a directory structure for your project:

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py
        asgi.py

Step 6: Navigate to the Project Directory

NOTE: For standard practice, I would recommend to rename myproject as an src. Then project structure would look like below:

src/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py
        asgi.py

Change into your project directory.

cd src

Step 7: Run the Development Server

Run the development server to verify that everything is set up correctly.

python manage.py runserver

You should see output similar to this:

(.venv) bikashmainali@Bikashs-MBP src % python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 28, 2024 - 19:57:55
Django version 5.0.6, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Open your web browser and go to http://127.0.0.1:8000/. You should see the Django welcome page.

Step 8: Create a Django App

In Django, an app is a web application that does something, such as a blog or a forum. Everything is considered as app. Create a new app within your project.

python manage.py startapp myapp

This will create a directory structure like this:

myapp/
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py
    migrations/
        __init__.py

Step 9: Add the App to Your Project

To include the new app in your project, add it to the INSTALLED_APPS list in myproject/settings.py:

myproject/settings.py

INSTALLED_APPS = [
    ...
    'myapp',
]

Step 10: Create a View

Create a simple view in myapp/views.py:

myapp/views.py

from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello, world!")

Step 11: Map the View to a URL

Create a urls.py file in your app directory and map the view to a URL:

 myapp/urls.py

from django.urls import path
from .views import hello

urlpatterns = [
    path('', hello, name='hello'),
]

Then include this URL configuration in the project's urls.py:

myproject/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

NOTE: There are other couple of ways of mapping urls (routing request)

Step 12: Run the Development Server Again

Run the development server again to see your changes.

python manage.py runserver

Navigate to http://127.0.0.1:8000/ in your web browser. You should see "Hello, world!".

Freeze python libraries:

Why requirements.txt

  1. Dependency Management:

    • It provides a centralized place to list all the dependencies your project needs, making it easier to manage and maintain them.
  2. Environment Replication:

    • It allows other developers to replicate the exact environment required for your project. This is essential for ensuring that the project runs smoothly in different environments, such as development, staging, and production.
  3. Version Control:

    • By specifying the exact versions of the dependencies, you can avoid compatibility issues. This ensures that your project uses the same versions of the libraries across different environments, which helps in debugging and testing.
  4. Simplifies Installation:

    • It simplifies the process of setting up the project. Instead of installing each dependency individually, you can install all dependencies listed in requirements.txt with a single command.
  5. Documentation:

    • It serves as a form of documentation that lists the libraries required for your project, providing insight into what tools and libraries the project depends on.
  6. Automated Deployment:

    • It facilitates automated deployment processes by providing a clear list of dependencies that can be installed programmatically.

Creating and Using requirements.txt

Creating requirements.txt

To create a requirements.txt file, you can manually list your dependencies or use the following command to automatically generate it based on the current environment:

pip freeze > requirements.txt

This command will capture all installed packages and their versions in your environment and write them to requirements.txt.

Example of requirements.txt

A requirements.txt file might look like this:

Django==3.2.4
requests==2.25.1
numpy==1.20.3

Installing Dependencies from requirements.txt

To install all the dependencies listed in requirements.txt, use the following command:

pip install -r requirements.txt

This command reads the file and installs the listed packages and their specified versions.