Django Create Project
Starting your first Django web application
🚀 What is a Django Project?
A Django project is a collection of settings and configurations for your web application. It serves as the container for your entire website, including databases, apps, and deployment settings.
# Create your first Django project
django-admin startproject myproject
cd myproject
python manage.py runserver
Key Project Components
Settings
Configuration for your project
DEBUG = True
ALLOWED_HOSTS = []
URLs
Route requests to views
urlpatterns = [
path('admin/', admin.site.urls),
]
WSGI
Web server gateway interface
application = get_wsgi_application()
Manage.py
Command-line utility
python manage.py runserver
🔹 Installing Django
Before creating a project, you need to install Django. Use pip, Python's package manager, to install Django on your system.
# Install Django using pip
pip install django
# Verify installation
python -m django --version
Output:
5.0.0
🔹 Creating Your First Project
Use the django-admin command to create a new project. This generates all necessary files and folders for your Django application.
# Create a new Django project
django-admin startproject mysite
# Navigate into the project
cd mysite
# Run the development server
python manage.py runserver
Output:
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
🔹 Project Structure Overview
After creating a project, Django generates several important files:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
- manage.py: Command-line tool for project management
- settings.py: Project configuration and settings
- urls.py: URL routing declarations
- wsgi.py: Entry point for WSGI servers
🔹 Running the Development Server
Django includes a lightweight development server for testing your application locally during development.
# Start server on default port (8000)
python manage.py runserver
# Start server on custom port
python manage.py runserver 8080
# Start server on specific IP and port
python manage.py runserver 0.0.0.0:8000
💡 Tip:
Visit http://127.0.0.1:8000/ in your browser to see the Django welcome page!
🔹 Basic Settings Configuration
The settings.py file contains important configurations for your project. Here are some essential settings you'll work with:
# settings.py
# Debug mode (set to False in production)
DEBUG = True
# Allowed hosts for your application
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
# Installed applications
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
# Database configuration
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}