Introduction to Django | Installation & Setup | Django & Database Connection – Technoinc
Introduction to Django
- What is Framework?
A framework is a collection of programs that do something useful and which can be used to develop your applications, dynamic web pages and database-driven websites. Normally they are written in a specific programming languages like Java, C#, Rails, PHP, Python etc
- Python Framework.
There are various python web framework which are given below.
- First category is Full-stack frameworks, in which we can use Django, Pyramid, TurboGears, Web2py.
- Second one is Microframeworks, in which we have options like Flask, Bottle, CherryPy.
- Third is Asynchronous frameworks, for that we can use Sanic, and Tornado.
Django: Django is a python based free and open source web application framework. which is nothing more than a collection of modules that make web development easier. They are grouped together and allow you to create application or websites from an existing source instead of development from scratch.
Install Django on windows:-
- To install Django first you have to check version of pip and than set virtual environment.
For checking pip version just type pip –version in command prompt and then type pip install virtualenvwrapper-win to install virtual environment.
- Now to create project make one virtualenv on your laptop.
I am going to make it on my desktop and name it first_project.
Now as shown in image I am working on my virtualenv first_project.
- Now time to install django. For that follow below steps.
- Now we create our first project name project1.
- Now on your given location project was created. You can open it in any editor like notepad ++ , visual studio code or sublime text or else you can use pycharm too.
You can see one manage.py file and one folder is your project folder.
Manage.py:- It is used for command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py. The inner mysite/ directory is the actual Python package for your project.
now if we look into our project files we can see mainly four python files which are
1.__init__py:- initially it is an empty file. It only contains version and sometimes I cane e used to import modules in it. If we are using any database than we are going to import database in this file like this.
2.settings.py:- This is a core file in Django projects. In settings.py there is one secret key. If you are going to put your project online than hide it or do not share it with anyone as it is use for encrypt your data.
-> In this file all the configuration values that our web app needs to work like database settings, logging configuration, where to find static files, API keys if you work with external APIs.
-> We can add our domain name in this file too at ALLOWED_HOST = [ ].
3.urls.py:- this file is used to route our application. It contains regular expressions and urlpatterns.
4. wsgi.py:- It’s commonly provided as an object named application in a Python module accessible to the server. The startproject command creates a file <project_name>/wsgi.py that contains such an application callable. It’s used both by Django’s development server and in production WSGI deployments.
This is all about file structure of Django project. Now for running our application type python manage.py run server in command prompt.
After entering that we get one server and port number which is highlighted here. Just copy it and paste it into your browser.
If you got following screen it means server is running properly and now you can add or edit HTML pages as you want to run your web application.
If you refresh your page or do any editing in code than on command prompt you get update like this.
If you want to work with database than you have to install it first. Here we are using my sql so just type pip install mysqlclient in command prompt.
Change database setting under settings.py file as follow if you are using my sql.
Than migrate all files to database by entering this line. Python manage.py migrate
After that you can create user login page or super user in phpmysql as you want and run again your server by typing python manage.py runserver.