What is Django?

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic
design. Built by experienced developers, it takes care of much of the hassle of web development,
so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

Django installation

For installation first you have to install python's latest version.
From here you can install python Install Python


To install Django in your system open cmd
press window+R

cmd
Enter the following command in cmd

pip install django

To check Django is installed or not enter the command in cmd

django-admin --version

if "django-admin --version" command in not working than re-enter the command "pip install django"
it may be happen that the django is installed but "django-admin" in not recoginized.In that case you have to install
virtual enviroment to install it type the given command in cmd

pip install virtualenv

env is stands for enviroment Check weather the virtual enviroment is installed or not type the given command

virtualenv --version

Now we are going to create the virtual enviroment to create it type the command given below:

virtualenv enviromentName

you can see the virtual enviroment is created in your working directory with your enviroment name

env

Now open cmd and change directory to the enviroment folder in the enviroment folder there will be a folder
with name "Scripts" go in the folder and type command "activate" to activate the virtual enviroment as you hit the enter button
you wiil see the enviroment is activated. You can see in the left of your directory the enviroment is visible

activate

Now we install the django inside the virtual enviroment type the command "pip install django", make sure that the virtual enviromentwhich we have created should be activated after installing django and now we start our project.


Start Django project

To start project type the given command

django-admin startproject projectName

I have named my project "Djangoproject". In the given image after the command you can see all the libraries have been created
in the Djangoproject folder. To start the project in the same folder in which we have enviroment folder we have to use the given command

django-admin startproject projectName .


Run the django server


To run the Django server use the command given below:

python manage.py runserver

As you run the server you see there is a server address provided in cmd

server

Copy the server address and open your browser than paste the server address in url like shown below:

activate

On etering the server address in browser you will reach to the official Django server page




Start the App


Now we are going to start our app forethere we have to open our command prompt and run the following command

django-admin startapp AppName

Now we have to open the django project in code editor. You can use any code editor you have. For django project I have used Visual Studio code (VScode). Open the project folder in the VScode and go to settings in project and scroll down find INSTALLED_APPS in settings define the app name I have named the app "DjangoApp"

env


Setup django URLS


For urls setup a urls.py file is present in project folder copy that file and paste it in app folder now go back in the project folder's urls file and include the path of app's urls file like shown in image

env

After including app urls path in project's urls we come to app folder than first we create template folder . In that folder we can create html files in templates I have created index.html file

env


Views Setup


In the application folder there is views.py file in which we will create our views. We have to define our views in views.py file.

env


Return methods in views


In views after deining a view we have to use return method. There are three type of return method available in django views the first one is "HttpResponse" method I have shown this method above in the image , second is "render" method that is default method in views this method render to the next selected html page after performing some operation like "return render('index')" and third method is "redirect" method redirect method redirect us in the same html page like "return redirect('index.html')"

Models in django

In the app folder there is models.py file available in which we can create our own model by creating a class we can create form model which we use in forms.py flie to create forms

env

Forms in django

We can create our forms like login form, signup form, registration form etc. For creation of a form first we have to create a python file with name "forms.py". We can import this forms in our views file and can use them on any page we'd like

env


Admin pannel in django

Django provide its own admin pannel and we can modify this pannel according to our need.Admin pannel is like database fo your website. We can cerate users or delelte them and update their profiles in database. All the form we have created first we have to import them in admin.py file which is already provided by django. So first we will create a admin for our website . Admin is the owner of the website for creating admin we have to go in command prompt and wirte the command given below:

Create Superuser

python manage.py createsuperuser

After createing admin or superuser we have to make migrations therefore we pass a command

Migrations in django

python manage.py makemigrations

python manage.py migrate

Now we will check our admin is created or not. To check we copy the server address from cmd and paste it into the browser after the address we add "/admin" to go into the admin pannel

env

env env


Here you see how easier to create a website/webapplication through Django.
I hope you got all the concepts in this Cheatsheet. if you have any problem
in django I have droped a link below of django documentation either you can
contactMe

Django Documentation