Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It follows the “don’t repeat yourself” (DRY) principle and emphasizes reusability and pluggability of components. It’s widely used in building complex, data-driven websites and web applications.
In this tutorial, we will cover the basics of Django and guide you through setting up a simple web application.
- Installation:
First, make sure you have Python installed on your system. You can download Python from the official website https://www.python.org/. Once you have Python installed, you can install Django using the following command:
pip install Django
- Creating a Django project:
To create a new Django project, run the following command in your terminal:
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
wsgi.py
manage.py
is a command-line utility that helps you manage your Django project. settings.py
contains the configuration settings for your Django project. urls.py
is the URL configuration for your project, and wsgi.py
is the entry point for WSGI-compatible web servers.
- Creating a Django app:
In Django, a project is made up of one or more apps. To create a new app, run the following command in your terminal:
python manage.py startapp myapp
This will create a new directory called myapp
inside your project directory with the following structure:
myapp/
migrations/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
models.py
is where you define your data models using Django’s Object-Relational Mapping (ORM). views.py
is where you define the logic for handling HTTP requests and returning HTTP responses.
- Configuring the database:
By default, Django uses SQLite as its database engine. If you want to use a different database engine, you can configure it in the settings.py
file by modifying the DATABASES
setting.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
You can replace sqlite3
with the database engine of your choice, such as PostgreSQL or MySQL.
- Running the development server:
To start the development server, run the following command in your terminal:
python manage.py runserver
This will start the development server on http://127.0.0.1:8000/
. You can access your Django project in a web browser by navigating to that URL.
- Creating views and URLs:
To create a view, open the views.py
file in your app directory and define a function that returns an HTTP response.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world!")
Next, create a URL pattern in the urls.py
file in your app directory that maps a URL to the view function.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index),
]
- Templates and static files:
Django allows you to create HTML templates and serve static files such as CSS, JavaScript, and images. To create a template, create a new directory called templates
inside your app directory and place your HTML files inside it.
To serve static files, create a directory called static
inside your app directory and place your static files inside it.
- Models and migrations:
Models in Django are defined using Python classes that subclass django.db.models.Model
. To create a new model, define a class in the models.py
file in your app directory.
After defining your models, you can generate database migrations by running the following command:
python manage.py makemigrations
And then apply the migrations to the database by running:
python manage.py migrate
This will create database tables based on your model definitions.
- Admin interface:
Django comes with a built-in admin interface that allows you to manage your database records. To enable the admin interface, open the admin.py
file in your app directory and register your models.
from django.contrib import admin
from .models import MyModel
admin.site.register(MyModel)
You can access the admin interface by navigating to http://127.0.0.1:8000/admin/
in your web browser.
- Deployment:
Finally, when you’re ready to deploy your Django project to a production environment, you can use a deployment platform such as Heroku, AWS, or Google Cloud Platform. Each platform has its own instructions for deploying Django applications, so be sure to follow the documentation for your chosen platform.
That’s it! You now have a basic understanding of Django and how to create a simple web application using the framework. Django has a rich ecosystem of libraries and tools that can help you build more complex applications, so be sure to explore the official Django documentation and tutorials for more information.