Hello, Welcome to our Django Tutorial. In this tutorial, we’ll cover everything about Django from basic to advanced. Before coming to the topic we hope you already have basic knowledge of the python programming language if you want to revise the concepts of python click here.
Django is a Python-based web framework that allows us to create and maintain quality web applications in a less complicated way. Django is easy to learn because of its built-in features like - Built-in admin panel, default databases, etc. By using Django we can create web applications in very less time.
Adrian Holovaty & Simon Willison created Django in the year 2003 While working in the Lawrence Journal-world newspaper when they started creating a web application using python. Django was officially released on 21, July 2005. Django is now an open-source project with a large community across the world.
Top Websites Built With Django
The Washington Post
Nasa Official Website.
Top 5 Advantages of using Django
- Django works on python: as we have mentioned before Django is fully written in python. python’s easy syntax structure makes it easy to use Django.
- Large Community of developers: As Django is very popular among developers it has a large community of developers. So, it makes it easy to find solutions to our problems.
- Follows DRY and KISS principles: Django follows the ‘KISS’ principle which stands for “keep it short and simple”. It means Django code must always be easy to understand and short. Similarly, it follows the ‘DRY’ principle which stands for “Do not repeat yourself”. This means we can reuse our Django code as many times as we want to use.
- REST Framework: REST (Representational State Transfer framework) is a powerful and flexible framework toolkit for building web APIs. it is a very powerful framework to build APIs easily.
- Very Secure: Django is one of the most secure web frameworks. In other frameworks, an experienced and skillful developer is required to achieve good security. But, using Django even a non-experienced person can build secure web applications.
Step-1 Installing Python
Django is fully written in Python so before installing Django first we have to install python in our system. To download python on your system click here.
If you are using Linux or Mac OS your system probably has python installed. Do check it before installation.
Step-2 Installing Code-Editor
There are a lot of code editors available. In this tutorial, I will use VS Code. To download VS Code click here.
Step-3 Installing Django
You can simply install Django by going to this website (https://www.djangoproject.com). You can install Django according to your OS. But in this tutorial, I will use a windows terminal to install it. Follow the steps given below to install Django.
Open VS Code.
Use the pip install Django command to install Django in your system.
\Desktop\Codersdaily> pip install django
By doing this Django will get installed on your system.
Creating Django Project
So, we have successfully installed Django to our system now, the next step we have to do is create a Django project. Use the following command to make your first Django project.
\Desktop\Codersdaily> django-admin startproject YourProjectName
After using this command a new folder will be added inside your project folder with some In-built files like:
- manage.py, __init__.py, asgi.py, settings.py, urls.py, and wsgi.py.
Creating Django App
After successfully creating our Django project, we have to create an app inside our Django project. The main difference between the Django project and the App is Project is the collection of configuration files & apps whereas, the app is built to perform logic. Use the following command to create an app.
\Codersdaily\project1> python manage.py startapp YourAppName
Now, a new folder with your app name will be added inside your project folder with some In-built files like - __init__.py, admin.py, apps.py, models.py, tests.py, and views.py.
Now, we have to add our app to project-installed apps. For this open your project’s setting.py file and add your app in the INSTALLED_APPS section.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'YourAppName', ]
Another thing you have to do is click on your app and create a urls.py file inside your app.
Running Django Project On Browser
After setting up all the basic things, let's test if our project is working or not. For this use the following command.
\Codersdaily\project1> python manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. October 13, 2022 - 15:16:52 Django version 4.1.2, using settings 'project1.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
So, no errors are detected. Now let's run our project by opening http://127.0.0.1:8000/
BOOM! Our Project is working.