Creating Django Apps

beginner
23 min

Creating Django Apps

Welcome to the Django Apps tutorial! This lesson is designed for beginners and intermediate learners who are eager to learn Django, a high-level Python web framework that enables rapid development of secure and maintainable websites. Let's dive in!

🎯 What is Django?

Django is a free and open-source web application framework written in Python. It simplifies the process of building complex, data-driven websites and applications, focusing on reusability and the "DRY" (Don't Repeat Yourself) principle.

πŸ“ Setting Up Django

To start, make sure you have Python and Pip installed on your system. Next, install Django using Pip:

bash
pip install django

Create a new Django project:

bash
django-admin startproject my_django_project

Navigate into your project folder:

bash
cd my_django_project

Now, let's create our first app within the project:

bash
python manage.py startapp my_app

πŸ’‘ Pro Tip:

In Django, a project can contain multiple apps. Each app is an isolated piece of the overall project, helping to keep the code organized and maintainable.

πŸ“ Structure of a Django App

Upon creating the my_app app, Django generates a default structure for you:

my_app/ β”‚ β”œβ”€β”€ migrations/ β”‚ β”œβ”€β”€ __init__.py β”‚ β”œβ”€β”€ admin.py β”‚ β”œβ”€β”€ models.py β”‚ β”œβ”€β”€ tests.py β”‚ β”œβ”€β”€ views.py β”‚ └── urls.py

Each file in this directory plays a crucial role in the functioning of your Django app.

🎯 Django Models

Models are Python classes that represent database tables. In Django, we define the structure of our data in models. Let's create a simple model for a Book:

python
# my_app/models.py from django.db import models class Book(models.Model): title = models.CharField(max_length=100) author = models.CharField(max_length=100) publication_year = models.IntegerField() description = models.TextField()

πŸ“ Note:

Remember, Django automatically handles creating, updating, and deleting the associated database table based on the structure defined in the model.

🎯 Django Views

Views are responsible for rendering the requested page to the user. In Django, views are Python functions or classes that return an HttpResponse object. Here's an example view that displays a list of books:

python
# my_app/views.py from django.http import HttpResponse from django.shortcuts import render from .models import Book def book_list(request): books = Book.objects.all() return render(request, 'book_list.html', {'books': books})

πŸ“ Note:

The render function in Django is a powerful shortcut that simplifies rendering templates and passing context data.

🎯 Django Templates

Templates are responsible for defining the structure and layout of the HTML pages in our application. Django uses the Jinja2 templating engine by default.

Create a new template file book_list.html under my_app/templates/my_app/:

html
<!-- my_app/templates/my_app/book_list.html --> <html> <head> <title>Book List</title> </head> <body> <h1>Book List</h1> <ul> {% for book in books %} <li>{{ book.title }} - {{ book.author }}</li> {% empty %} <li>No books found.</li> {% endfor %} </ul> </body> </html>

πŸ“ Note:

The {% for %} and {% empty %} tags are used for iterating over a list of objects and handling empty lists, respectively, in Django templates.

🎯 Running Your Django App

Now that we have our Django app set up, let's run it:

bash
python manage.py runserver

Access your Django app in your web browser at http://127.0.0.1:8000/

πŸ“ Note:

Django provides a built-in development server for quick testing and debugging.

πŸ’‘ Pro Tip:

Django offers many powerful features like authentication, admin interface, and form handling, making it an excellent choice for web development.

🎯 Quiz

Quick Quiz
Question 1 of 1

What does the `render` function in Django do?

That's it for our Django Apps tutorial! Now that you've learned the basics of creating Django apps, it's time to start building your own projects. Happy coding! πŸŽ‰πŸ»