| DJC | Django | Introduction |

Django Installation and Basic Setup - DJC

Before starting to use Django, you need to set up your environment and install the necessary tools.

1. Prerequisites

Before installing Django, make sure you have the following installed on your machine:

  • Python 3.x: Django is compatible with Python 3. Check if Python is installed by running:
python --version

If it’s not installed, you can download and install Python from python.org.

  • pip: This is Python’s package installer. Verify if it’s installed by running:
pip --version

2. Setting Up a Virtual Environment

It’s recommended to create a virtual environment to manage dependencies for each Django project separately. A virtual environment isolates your project’s Python packages from those installed system-wide.

To create and activate a virtual environment:

Install the venv module if you don’t already have it:

python -m pip install --user virtualenv

Create a virtual environment:

python -m venv myenv

Replace myenv with the name of your virtual environment.

Activate the virtual environment:

  • On Windows:
myenv\Scripts\activate
  • On macOS and Linux:
source myenv/bin/activate

When the virtual environment is active, you’ll see its name in your terminal prompt, indicating that all installations are now isolated within that environment.

3. Installing Django

With the virtual environment activated, install Django using pip:

pip install django

To verify the installation, run:

django-admin --version

This should display the installed Django version.

4. Creating Your First Django Project

Now that Django is installed, you can create a new Django project. Navigate to the directory where you want to create it and run:

django-admin startproject myproject

This will create a new directory called myproject with the following structure:

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
  • manage.py: Command-line utility to interact with your project (e.g., run the server, create migrations).
  • settings.py: Contains all your project’s configuration.
  • urls.py: Defines your project’s URL routing.
  • wsgi.py and asgi.py: Entry points for WSGI/ASGI-compatible web servers.

5. Creating a Django App

Django promotes a modular approach, allowing one Django installation to contain multiple apps. To create a new app within your project, follow these steps:

Navigate to your project directory:

cd myproject

Create a new app using the following command:

python manage.py startapp myapp

This will create a new directory called myapp with the following structure:

myapp/
    migrations/
        __init__.py
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py
  • migrations/: Folder to handle database migrations.
  • admin.py: Used to register models in the Django admin panel.
  • apps.py: App configuration file.
  • models.py: Define your app’s data models here.
  • tests.py: Write tests for your app here.
  • views.py: Contains your app’s views.

Register the App

For Django to recognize your new app, you must add it to the project’s configuration. Open the settings.py file and locate the INSTALLED_APPS section. Add your app to the list:

INSTALLED_APPS = [
    ...
    'myapp',
]

6. Running the Development Server

To verify everything is set up correctly, navigate to your project folder:

cd myproject

Run the following command to start the development server:

python manage.py runserver

You should see output like this:

Watching for file changes with StatReloader
Performing system checks...

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 Django’s “It worked!” page, confirming your project is running.

7. Basic Configuration

Before continuing with development, it’s a good idea to adjust a few basic settings in settings.py:

  • Time Zone: Set your local time zone:
TIME_ZONE = 'America/Lima'

Replace 'America/Lima' with your local time zone.

  • Language: Set the default language:
LANGUAGE_CODE = 'es-es'

For English, use 'en-us'.

  • Allowed Hosts: When deploying your project, specify the domains or IP addresses allowed to access your site:
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

These basic configurations prepare your project for local development. As you progress, you’ll make further adjustments for security, databases, and other settings.

| DJC | Django | Introduction |