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!
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.
To start, make sure you have Python and Pip installed on your system. Next, install Django using Pip:
pip install djangoCreate a new Django project:
django-admin startproject my_django_projectNavigate into your project folder:
cd my_django_projectNow, let's create our first app within the project:
python manage.py startapp my_appIn 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.
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.
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:
# 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()Remember, Django automatically handles creating, updating, and deleting the associated database table based on the structure defined in the model.
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:
# 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})The render function in Django is a powerful shortcut that simplifies rendering templates and passing context data.
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/:
<!-- 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>The {% for %} and {% empty %} tags are used for iterating over a list of objects and handling empty lists, respectively, in Django templates.
Now that we have our Django app set up, let's run it:
python manage.py runserverAccess your Django app in your web browser at http://127.0.0.1:8000/
Django provides a built-in development server for quick testing and debugging.
Django offers many powerful features like authentication, admin interface, and form handling, making it an excellent choice for web development.
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! ππ»