Django is a high-level web framework for Python that encourages rapid development and clean, pragmatic design. In this article, we'll explore the basics of Django and guide you through creating a simple web application. Whether you're a beginner in web development or looking to leverage Python for building robust web applications, Django is an excellent choice.
Installing Django
Before we start building our web application, we need to install Django. Open your terminal or command prompt and run the following command:
pip install django
This command installs the latest version of Django on your machine.
Creating a Django Project
Once Django is installed, let's create a new project. In your terminal, run the following commands:
django-admin startproject myproject
cd myproject
This creates a new Django project named "myproject." The project structure includes necessary files and directories for your web application.
Creating a Django App
Django projects are made up of apps. Let's create a simple app for our project. In the terminal, run:
python manage.py startapp myapp
This creates a new app named "myapp" within your project. Apps are modular components that help organize your project's functionality.
Defining Models and Database Setup
In Django, models represent your database structure. Open the
models.py file inside your "myapp" directory and define a
simple model:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
def __str__(self):
return self.title
After defining your model, apply the changes to the database by running:
python manage.py makemigrations
python manage.py migrate
This sets up your database with the defined model.
Creating Views and Templates
Django follows the Model-View-Controller (MVC) architectural pattern. Views
handle the logic of your application, and templates define the presentation.
Create a file named views.py in your "myapp" directory:
from django.shortcuts import render
from .models import Book
def book_list(request):
books = Book.objects.all()
return render(request, 'myapp/book_list.html', {'books': books})
Create a folder named "templates" inside your "myapp" directory. In this
folder, create a file named book_list.html:
<h1>Book List</h1>
{% for book in books %}
<p>{{ book.title }} by {{ book.author }}</p>
{% endfor %}
This template will display a list of books. Now, define a URL pattern to
connect your view. Create a file named urls.py in your "myapp"
directory:
from django.urls import path
from .views import book_list
urlpatterns = [
path('books/', book_list, name='book_list'),
]
Lastly, include this URL pattern in the project's
urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
Running the Development Server
Now that your project is set up, run the development server:
python manage.py runserver
Visit http://127.0.0.1:8000/books/ in your web browser, and you
should see your book list.
Conclusion
Congratulations! You've successfully created a simple web application with Django. This is just the beginning of what Django can offer. As you continue exploring, you'll discover more features for handling forms, user authentication, and building dynamic web applications.
Stay tuned for future articles where we'll explore advanced Django features and real-world web application development.