What is Django ?
Django is web framework written in python for rapid development of data-driven webapp. The primary goal and principle of django is DRY("don't repeat yourself").
Django is a much heavier weight web framework than Flask with a lot more out-of-the-box features so you can focus on writing your app without needing to reinvent the wheel. By default, django prevents most common security mistakes like XSS, CSRF, SQL injection etc. Django is a framework for perfectionists with deadlines.
Django is a much heavier weight web framework than Flask with a lot more out-of-the-box features so you can focus on writing your app without needing to reinvent the wheel. By default, django prevents most common security mistakes like XSS, CSRF, SQL injection etc. Django is a framework for perfectionists with deadlines.
*Create Django Project
- python (python3 is recommended)
- django ( at least version 2.0 or higher)
- text editor (atom is recommended because its works almost same on different operating system)
Django divides all of its web applications into projects, composed of different parts.
To create and start a new project, run the command in terminal.
- django-admin startproject projectname
where projectname is the name of your project.
Django is built on the idea of packages. A web application can be made of multiple packages, each serving a slightly different purpose, and Django will help manage these.
Project Components
- Django creates a number of files within projectname directory
- __init__.py : defines the directory projectname as a Python package, collection of multiple Python files.
- settings.py : basic settings, like time zone, other applications installed in the project, what sort of database is used, etc.
- urls.py: determine what URLs/route can be accessed when using the web application.
- wsgi.py: a file that helps to deploy an application to a web server.
- manage.py: a Python script that can be used to perform useful operations on web application.
- A Django project consist of one or more Django applications, or apps, which serves a particular purpose.
Create A Basic Application:
To create an app, inside the project directory, run the following command.
- python3 manage.py startapp appname
where appname is the name of your application. This command create a directory appname inside of .
project directory. This directory contains the number of files automatically.
Inside appname directory
- views.py: It contains the code that determines what the user sees at a particular route or url.
- apps.py: contains the configurations for app.
- admin.py: For admin related task in the app.
- tests.py: It contains the code for testing app whenever new feature is added on app
- models.py: Its conatins the models that create the database.
In the above image of text editor, article is project name and inside article demo is my app name.
Running Web Server
run the following command in project directory.
- python3 manage.py runserver
now server is start on your localhost. Follow link in your browser to see default page.