Installation and Basic Setup of Django
Before starting to use Django, it's necessary to set up the environment and install the required 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 you have Python 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 is installed by running:
pip --version
2. Setting Up a Virtual Environment
It’s recommended to create a virtual environment to manage the dependencies of each Django project separately. A virtual environment isolates your project's Python packages from those installed globally on the system.
To create and activate a virtual environment:
Install the venv
module if you don’t 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 activated, you'll see its name in the terminal prompt, indicating that all installations are now isolated within the virtual environment.
3. Installing Django
With the virtual environment activated, you can install Django using pip:
pip install django
To verify the installation, run:
django-admin --version
This should display the version of Django installed.
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 the project 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
5. Creating a Django Application
Django promotes a modular approach, allowing a single Django installation to contain multiple applications. To create a new application within your project, follow these steps:
Navigate to your project directory:
cd myproject
Create a new application 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
Register the Application
To make Django recognize your new application, you need to add it to the project’s settings. Open the settings.py
file and find the INSTALLED_APPS
section. Add your application to the list:
INSTALLED_APPS = [
...
'myapp',
]
6. Running the Development Server
To verify that 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
Open your web browser and go to http://127.0.0.1:8000/
. You should see Django's "It worked!" page, confirming that your project is running.
7. Basic Configuration
Before proceeding with development, it’s recommended to adjust some basic settings in settings.py
:
- Time Zone: Set your local time zone:
TIME_ZONE = 'America/Lima'
- Language: Set the default language:
LANGUAGE_CODE = 'es-es'
- Allowed Hosts: When deploying your project, specify the domains or IP addresses that can access your site:
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']