Byte Squad

Django Getting Started: Building Web Applications with Ease

August 26, 2023 | by bytessquad.com

Web development using Django

In the world of web development, building robust and dynamic web applications is a demanding task. Thankfully, frameworks like Django exist to simplify the process and allow developers to focus on creating innovative features rather than dealing with the nitty-gritty details of backend development. In this article, we will embark on a journey to explore Django – a high-level Python web framework – and learn how to get started with building web applications effortlessly.

What is Django?

Django is an open-source, high-level Python web framework that follows the Model-View-Controller (MVC) architectural pattern. It encourages rapid development and clean, pragmatic design by providing a plethora of built-in features and tools. Developed in 2003 by Adrian Holovaty and Simon Willison, Django has since become a popular choice for building all kinds of web applications – from simple blogs to complex e-commerce platforms.

Setting Up Django

Before we delve into creating web applications, we need to set up Django on our development environment. The following steps will guide you through the installation process:

  1. Install Python: Ensure you have Python installed on your system. You can download it from the official Python website.
  2. Virtual Environment (Optional but Recommended): It’s a good practice to create a virtual environment for your Django projects. This helps in keeping project dependencies isolated. To create a virtual environment, use the following command:
   python3 -m venv myenv
  1. Install Django: Once you have your virtual environment set up (if you’re using one), you can install Django using pip:
   pip install django
  1. Verify Installation: To verify that Django is installed correctly, you can run the following command to check the version:
   django-admin --version

Creating a Django Project

Let’s create a simple Django project named “MyWebsite” as an example to understand the basic structure and concepts of Django.

  1. Start a Project: Navigate to your desired project directory and run the following command:
   django-admin startproject MyWebsite

This will create a directory named “MyWebsite” with the initial project structure.

  1. Project Structure:
   MyWebsite/
   ├── manage.py
   └── MyWebsite/
       ├── __init__.py
       ├── settings.py
       ├── urls.py
       └── wsgi.py
  • manage.py: Command-line utility to interact with the project.
  • MyWebsite/: This is the actual Python package of your project.
    • settings.py: Configuration settings for your project.
    • urls.py: URL routing configuration.
    • wsgi.py: Entry point for the WSGI (Web Server Gateway Interface) server.

Running the Development Server

Django comes with a built-in development server that allows you to test your application during development. To start the server, navigate to your project directory where manage.py resides and run:

python manage.py runserver

By default, the server runs on http://127.0.0.1:8000/. Open this URL in your browser, and you should see the default Django welcome page.

Creating an App

In Django, a project is made up of multiple apps, each serving a specific purpose. Let’s create an app named “blog” within our project.

  1. Create the App: Run the following command:
   python manage.py startapp blog
  1. App Structure:
   blog/
   ├── migrations/
   ├── __init__.py
   ├── admin.py
   ├── apps.py
   ├── models.py
   ├── tests.py
   └── views.py
  • models.py: Define your data models using Django’s Object-Relational Mapping (ORM).
  • views.py: Create views, which handle HTTP requests and return responses.
  • admin.py: Register your models to make them accessible via the Django admin interface.

Creating Views and Templates

Let’s create a simple view that displays a list of blog posts.

  1. Update views.py:
   from django.shortcuts import render
   from .models import Post

   def post_list(request):
       posts = Post.objects.all()
       return render(request, 'blog/post_list.html', {'posts': posts})
  1. Create a Template: Create a directory named templates within the blog app directory. Inside the templates directory, create another directory named blog. Then, create a file named post_list.html within the blog directory.
   <!DOCTYPE html>
   <html>
   <head>
       <title>Blog Post List</title>
   </head>
   <body>
       <h1>Blog Posts</h1>
       <ul>
           {% for post in posts %}
               <li>{{ post.title }}</li>
           {% endfor %}
       </ul>
   </body>
   </html>

URL Mapping

To access the view we created, we need to map URLs to views.

  1. Update urls.py in the blog app:
   from django.urls import path
   from . import views

   urlpatterns = [
       path('', views.post_list, name='post_list'),
   ]
  1. Update urls.py in the project directory:
   from django.contrib import admin
   from django.urls import path, include

   urlpatterns = [
       path('admin/', admin.site.urls),
       path('', include('blog.urls')),
   ]

Running the Application

After creating views, templates, and setting up URL mappings, you can restart the development server and access your application at http://127.0.0.1:8000/. You should see the list of blog posts displayed on the page.

Conclusion

Django provides an elegant and efficient way to build web applications by simplifying complex tasks and promoting rapid development. In this article, we’ve just scratched the surface of Django’s capabilities. As you delve deeper, you’ll discover features like authentication, forms handling, database migrations, and much more. With Django as your ally, the journey of creating sophisticated web applications becomes not just manageable, but enjoyable. Happy coding!

RELATED POSTS

View all

view all