Getting Started with Python Django: A Beginner’s Tutorial

Posted by


Django is a high-level web framework written in Python that encourages rapid development and clean, pragmatic design. It’s a popular framework used by developers to build web applications quickly and efficiently. In this tutorial, we will cover the basics of Django and help you get started with building your first web application.

Installing Django:

Before we get started with Django, we need to install it on our system. Django can be easily installed using pip, which is the package installer for Python. To install Django, open your terminal or command prompt and run the following command:

pip install django

This will install Django on your system and you will be ready to start building web applications.

Creating a Django Project:

To create a new Django project, run the following command in your terminal:

django-admin startproject myproject

Replace ‘myproject’ with the name of your project. This command will create a new directory with the project structure and files needed for a Django project.

Running the Development Server:

To run the development server and test your Django project, navigate to the project directory and run the following command:

python manage.py runserver

This will start the development server and you can access your Django project by opening a web browser and entering the URL http://localhost:8000/.

Creating a Django App:

In Django, a project is composed of one or more apps. To create a new app within your project, run the following command in your terminal:

python manage.py startapp myapp

Replace ‘myapp’ with the name of your app. This command will create a new directory with the app structure and files needed for a Django app.

Creating Models:

In Django, models are used to define the structure of your database tables. You can create models by defining classes in your app’s models.py file. Here is an example of a simple model definition:

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)
    published_date = models.DateField()

    def __str__(self):
        return self.title

In this example, we have defined a Book model with three fields: title, author, and published_date.

Creating Views:

Views in Django are responsible for processing user requests and returning responses. You can create views by defining functions in your app’s views.py file. Here is an example of a simple view that returns a list of books:

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})

In this example, we have defined a book_list view that retrieves all the books from the database and passes them to a template called book_list.html.

Creating Templates:

Templates in Django are used to generate HTML pages dynamically. You can create templates by creating HTML files in your app’s templates directory. Here is an example of a simple template for displaying a list of books:

<!DOCTYPE html>
<html>
<head>
    <title>Book List</title>
</head>
<body>
    <h1>Book List</h1>
    <ul>
        {% for book in books %}
            <li>{{ book.title }} - {{ book.author }}</li>
        {% endfor %}
    </ul>
</body>
</html>

In this example, we have created a simple HTML template that iterates over a list of books and displays the title and author of each book.

Setting up URLs:

URLs in Django are used to map user requests to views. You can define URL patterns by creating a urls.py file in your app directory. Here is an example of how you can set up URLs for your app:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.book_list, name='book_list'),
]

In this example, we have defined a URL pattern that maps the root URL to the book_list view.

Running Migrations:

After creating models in your Django app, you need to run migrations to create database tables for your models. You can do this by running the following command in your terminal:

python manage.py makemigrations
python manage.py migrate

This will create database tables based on the models you have defined in your app.

That’s it! You have now created a simple Django project with models, views, templates, and URLs. Django provides a powerful framework for building web applications and has a vibrant community of developers. I hope this tutorial has helped you get started with Django and feel more comfortable exploring its features and capabilities. Happy coding!

0 0 votes
Article Rating
25 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@narendranrammudo6726
1 month ago

@17:43 it should be 'DIRS': ['myproject/templates'],

@riteshchavan8308
1 month ago

It was very easy to understand than other videos out there.
Thanks.
Hope I will continue with the series.

@BilalBashir-z5s
1 month ago

Excellent and Commendable

@kirillzlobin7135
1 month ago

What is the difference between py -m pip install django and pip install django

@chrishicks7630
1 month ago

Excellent tutorial. many thanks.

@trashrummager
1 month ago

26:19 when i check elements on the page it says err aborted 404.

@VoiceOfAsh
1 month ago

This is the fastest, I have ever learned from a YouTube video. Thank you, Dave! What I also, appreciate is that you spell out the shortcut keys. That saves me time to watch a separate video to learn about the shortcut keys, say for VS Code.

@jayanspaliwal5907
1 month ago

Hey, Dave, kindly pin this comment- For all windows users, if you get error on using the source command, just type '..venvScriptsActivate' and it'll work fine

@gwynedd-1
1 month ago

I can't get past use of Ctl-C (on mac) after that step my venv is stuck and won't let me deactivate or runserver. I have deleted all and started from scratch 3 times and the same thing happens everytime after the Ctl-C command.

@dexterpacaldo
1 month ago

Very clear and clean, Thank you for sharing.

@deanwinchester8943
1 month ago

Hello Dave , i have this problem
The term 'source' is not recognized as the name of a cmdlet, function,

script file, or operable program. Check the spelling of the name, or if a path

was included, verify that the path is correct and try again.
could you or someone here help with that

@automaton6352
1 month ago

if we know react js, do we need to take python course?

@chemistry9085
1 month ago

Thank you very much

@SubaydaAbdi
1 month ago

Thankyou for your help❤

@MTran-r8c
1 month ago

Thanks a lot! This video is helpful! ♥

@DavidBennett-ez2yz
1 month ago

Thanks!

@yonas_jmhone
1 month ago

"Source : The term 'Source' is not recognized as the name of a cmdlet, function, script file, or operable program" I am getting this error message when I execute source .venv/Scripts/activate.

@YussefMahmoud-i2d
1 month ago

Thanks, what screen recorder you use ?

@heychaklader
1 month ago

I don't understand why the sub is still not over 1 million – this is plain wrong IMO

@tweetybird155
1 month ago

hello, how come u were able to run the project without an error? i copied what you did but my terminal displayed AttributeError: module 'myproject.views' has no attribute 'homepage' same thing with the 'about' 😭😭