Getting Started With Django

Here we will learn the essential information on Django. So let’s start, one question that’s always raised in everyone’s mind before learning a new thing is what basically it is? So let me answer your question.

What is Django?

Django is a high-level, web framework written in Python for rapid development and pragmatic design. A framework is simply a collection of modules that are grouped together for creating web applications from a reliable, pre-existing existing source. Django offers a large collection of these modules.

Django’s primary goal is to ease the creation of complex database-driven websites. Some well-known sites that use Django include PBS, Instagram, Disqus, Washington Times, Bitbucket, and Mozilla.

How does Django work?

In Django, we can make projects and applications. In most instances, the terms can be interchangeable, but in Django, they are not. Projects can contain several smaller applications that serve a particular function or purpose.

Step 1: Install Django using the following command

python -m pip install Django

Verify installation with the following command:

python -m django --version

Step 2: Initialize Project

Django project is a collection of applications and configurations. The following command will create a Django project in the django_project directory.

django-admin startproject django_project
  1. In your virtual environment command line, run the above command. This creates the workspace that will encapsulate all of your applications and your config files.
  2. cd into django_project.
  3. Run command line to python manage.py runserver and open up localhost:8000 to see if you get a site with an animated rocket that tells you your install worked.

Step 3: what is makemigrations and migrate in Django.

makemigrations:- which is responsible for creating new migrations based on the changes you have made to your models.

  • makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created apps’ model which you add in installed apps. It does not execute those commands in your database file. So tables doesn’t create after makemigrations.

Before adding anything specific to your application, you should also run Django’s initial migrations.

python manage.py makemigrations

migrate:- migrate executes those SQL commands in a database file. So after executing migrate all the tables of your installed apps are created in your database file.

python manage.py migrate

Step 4: Create an Application in our Django project.

  • In your command prompt run the below command
python manage.py startapp django_app
  • Configure the application in the project. Look for the INSTALLED_APPS variable in the settings.py file of the django_project folder. After the final application in the list, add the name of our application as a string.

  • Import the HttpResponse package from the HTTP Django module to create a view, and create a view function in our django_app.
from django.http import HttpResponse

def index(request):
    return HttpResponse(“Hello World!”)
  • Use Project URL Mapper to route to application view:
  • Navigate to django_project/urls.py
  • Import from django_app import views at the top of the file
  • In the urlpatterns list, add the following command.
path('',views.index,name="index")

  • execute Project: Runpython manage.py runserver and open up localhost:8000 to see the result of our example!

Step 5: Create a superuser for Django default admin panel access to run the below command in your terminal and provide the required details for creating a superuser.

python manage.py createsuperuser

Step 6: Go to http://localhost:8000/admin/  url and provide your username and password to access the Django admin panel.

To learn more about Django, check the official documentation.

I hope you’ll get impressed by the features of Django. So let’s start coding within the next blog. If you’ve got any questions regarding this blog, please let me know in the comments.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories