Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. This guide will walk you through setting up your first Django project, creating a virtual environment, installing Django, and running your development server.
A virtual environment is an isolated environment for Python projects. It ensures your project dependencies are maintained separately.
python -m venv <environment_name>- Replace
<environment_name>with your preferred environment name (commonly.venvorenv).
Example:
python -m venv .venvOn Linux/macOS:
source .venv/bin/activateOn Windows:
.venv\Scripts\activateWhen activated, your terminal prompt should change, indicating you're now inside the virtual environment.
With your virtual environment activated, install Django using pip:
pip install DjangoYou can verify the Django installation with:
python -m django --versionTo create a new Django project, use the django-admin tool:
django-admin startproject my_website- This command creates a new directory called
my_websitecontaining the initial project structure.
Directory structure:
my_website/
manage.py
my_website/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
Tip: You can replace
my_websitewith your desired project name.
Change directory into your project folder (e.g., jinjaTesting):
cd jinjaTestingReplace
jinjaTestingwith your actual project folder name.
Start the built-in development server with:
python manage.py runserver- By default, the server runs at
http://127.0.0.1:8000/. - Open this URL in your browser to see the Django welcome page.
Additional Options:
- To run the server on a different port:
python manage.py runserver 8080
- To specify an IP and port:
python manage.py runserver 0.0.0.0:8000
Now that your Django project is running, you can:
- Create apps within your project using
python manage.py startapp <app_name>. - Define models, views, and templates.
- Configure URLs and settings.
- Use Django's ORM, authentication system, and admin interface.
| Command | Description |
|---|---|
python manage.py migrate |
Applies database migrations |
python manage.py createsuperuser |
Creates an admin user |
python manage.py startapp <app_name> |
Starts a new Django app |
python manage.py makemigrations |
Prepares changes to your models for migration |
python manage.py shell |
Opens a Python shell with Django context |
# Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate
# Install Django
pip install Django
# Start a new Django project
django-admin startproject jinjaTesting
# Move into your project directory
cd jinjaTesting
# Run the development server
python manage.py runserverYou should now be able to access your Django project in your web browser!