Django Introduction 🎯

beginner
13 min

Django Introduction 🎯

Welcome to Django, a powerful Python web framework that simplifies the process of building dynamic websites. Let's dive into the world of Django together!

Why Django? πŸ“

Django offers several advantages:

  1. Rapid Development: Django follows the DRY (Don't Repeat Yourself) principle, promoting code reusability and faster development.
  2. Secure: Django comes with built-in security features like Cross-Site Scripting (XSS) protection, Cross-Site Request Forgery (CSRF) protection, and SQL injection prevention.
  3. ** scalable**: Django can handle high traffic and is suitable for both small and large projects.
  4. Python-based: Being Python-based, Django is easy to learn and has a vast community for support.

Installing Django βœ…

To install Django, use pip, the Python package manager:

bash
pip install django

Creating a Django Project πŸ’‘

Create a new Django project using the following command:

bash
django-admin startproject my_first_project

Change to the newly created project directory:

bash
cd my_first_project

Creating a Django App πŸ’‘

Create a new Django app within the project:

bash
python manage.py startapp my_first_app

Now, let's write our first view!

Writing a View πŸ’‘

Views in Django handle the logic of what to display in a web page. To create a simple view, edit the views.py file in your app directory:

python
from django.http import HttpResponse def home(request): return HttpResponse("Welcome to my_first_app!")

Now, let's make this view accessible.

URL Mapping πŸ’‘

Edit the urls.py file in your app directory to include the view:

python
from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), ]

Now, run your server:

bash
python manage.py runserver

Visit http://127.0.0.1:8000/ in your browser, and you should see the message "Welcome to my_first_app!"

Quiz Time 🎯

Quick Quiz
Question 1 of 1

What does Django use for handling the logic of what to display in a web page?

Django Models πŸ’‘

Models in Django represent database tables and their fields. To create a simple model, edit the models.py file in your app directory:

python
from django.db import models class MyModel(models.Model): name = models.CharField(max_length=100) description = models.TextField()

Let's create a database migration and apply it:

bash
python manage.py makemigrations python manage.py migrate

Now, let's create a view to display and manipulate the data.

Quiz Time 🎯

Quick Quiz
Question 1 of 1

In Django, what do models represent?

Conclusion πŸ“

You've taken your first steps in the world of Django! By understanding views, URL patterns, and models, you've laid a solid foundation for your web development journey.

In the next lesson, we'll dive deeper into Django templates and forms, making our web applications more interactive and user-friendly. Stay tuned! 🎯