Django
As we are talking about open source , Django being one , is high-level python backend framework which is not only open source but also offers us a number of features which serves as crucial part for an application . It is Built and forged by skilled and experienced developers.
Features that make Django , a top rated backend framework
- Django with its predefined and marshal arrangement of things, makes it flexible and also provides rapid development.
- Security is a primary concern for every professional developer , hence django provides various unbeatable features starting SQL Injection to cross-site scripting to keep things under the shelter of security.
- Being scalable and versatile , make django easy to use and hence can easily back both large scale and small scale applications.
- Django contains various helping task modules and libraries which can be used to handle common Web development tasks.
High-profile site that use Django include: Disqus , Instagram , Knight Foundation , Mozilla , National Geographic etc.
Installation
1. Install Python in your root directory
Install Python
pip install python
2. Create a Virtual Environment
python3 -m venv envname
3. Activate Virtual Environment
source envname/bin/activate
4. Install Django
pip install django
5. Create a new Django Project
Create new django project by running the following command.
django-admin startproject projectname
This will create a mysite directory in your current directory. Get inside that directory using the command line.
After locating and getting into the project directory you will this kind of project structure.
projectname/
manage.py
projectname/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
6. Running the Django Project
python manage.py runserver
7. Create an Admin user
python manage.py createsuperuser
About Django Admin Panel
Django provides built-in admin module to monitor all the CRUD Operations on the django tables(models).It provides a user-friendly interface where user can manage the content and data of the application.
The admin app (django.contrib.admin) is enabled by default and already added into INSTALLED_APPS section of the settings file of the project.
Ways to access admin panel
1. After checking all the configurations in the setting file, again run the server.
python manage.py runserver
2. Apply migrations
By default , some changes are not migrated in the django project , hence to perform those changes , use the following commands.
python manage.py makemigrations
python manage.py migrate
3. Use the /admin/ extension after your localhost url to access the panel page
4. Login in into the panel using the admin credentials .
and voila, this is your admin-panel
Create Django App
Django application comprises various projects and app. It also generates an automatic base directory.
1. Steps to create django app
python manage.py startapp appname
2. Register the app in your settings file
All the configurations are stored in settings.py file of the main project hence it is mandatory to register the app under the INSTALLED_APP settings of the project.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'appname',
]
File Structure and its uses
Django has a hierarichal file structure which is easy to understand and work with . Its the file structure that makes django flexible to learn for beginners.
1. Settings file
Settings.py is the first file which is run when a project runs. This file contains the all the configurations starting from server and host setting to database and template settings.If you work with external apis or you want to integrate ml model in your project then , its settings.py file that comes into play first.
Fields in settings file
- Database settings
- Template settings
- Host settings
- login configurations
- static file settings
- Admin settings
- Api and other stuff
2. Models File
Django works on MVT(Model View Template) software design pattern hence we tend to list all the tables required in the project . Model files contains table names and essential field for that respective table.
Django Model is a subclass of django.db.models.Model and each field of the model class represents a database field (column).
Django provides us a database-abstraction API which allows us to create, retrieve, update and delete a record from the mapped table.
Here is an example of the model
class user(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField(max_length=100)
Some of the fields which we can use while creating models.
- AutoField
- BigAutoField
- BigIntegerField
- BinaryField
- BooleanField
- ImageField
- CharField
- IntegerField
- EmailField
- DateTimeField
3. Django Views
Django view comprise views.py file where we can put all our business logic . This business logic tends work out various functionalities and logics of the application and return response to the user
4. Urls
Views comes with urls. We need to route a view so as to see its response hence it is important to route a url in urls.py file of the project
path('index/',index,name=index)
5. Templates
Python being a dynamic backend framework needs hypertext markup language to render the data and present it for the user's to see.Django template engine is used to separate the design from the python code and allows us to build dynamic web pages.
For templates to function, we need to add it in INSTALLED_APPS section of the settings file
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
And , here we are , starting and working with top rated and flexible backend framework.Everything technology needs to researched carefully hence if you want to learn more about this amazing frameworks , just head towards its documentation.
Thank You!