Django


With Django, you can take Web applications from concept to launch in a matter of hours. Django 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. (docs)

Django Introduction

Django follows the MVC pattern closely, however it does use its own logic in the implementation. Because the “C” is handled by the framework itself and most of the excitement in Django happens in models, templates, and views, Django is often referred to as an MTV framework. In the MTV development pattern:
M stands for “Model” the data access layer. This layer contains anything and everything about the data: how to access it, how to validate it, which behaviors it has, and the relationships between the data.
T stands for “Template” the presentation layer. This layer contains presentation-related decisions: how something should be displayed on a Web page or other type of document.
V stands for “View” the business logic layer. This layer contains the logic that accesses the model and defers to the appropriate template(s). You can think of it as the bridge between models and templates.
Django is not just limited to MTV, we can use any other front-end framework(React, Angular) instead of using base design pattern.
Many of us use Django to build Rest APIs. as Django written in python so it has a lot of flexibilities.
The best thing I like about Django is its Admin panel to maintain models.
Django admin can easily be customized, like uploading a file via admin(action in the list view of a model) and many more.

Writting first Django Views (Hello World!)

Before start writing our first view by creating a Django project.
django-admin startproject helloworld. 
This will create some folder structure like below

helloworld/
    manage.py
    helloworld/
        __init__.py
        settings.py
        urls.py
        wsgi.py
Create a new app which contains your views.
python manage.py startapp polls

This will create some more file for your app. structure is below
polls/
    __init__.py
    admin.py
    apps.py
    migrations/
         __init__.py
    models.py
    tests.py
    views.py
Now let us start writting our view. Like in the polls app open up view.py and write bellow codes
from django.http import HttpResponse
def hello(request):
    return HttpResponse("Hello world")
Lets see what we wrote up:


  • First, we import the class HttpResponse, which lives in the django.http module. We need to import this class because it’s used later in our code.
  • Next, we define a function called hello – the view function.
  • Each view function takes at least one parameter, called request by convention. This is an object that contains information about the current web request that has triggered this view, and is an instance of the class django.http.HttpRequest.
  • Now we has define a url for this view. open up urls.py inside polls. and write the url conf for above view. This will bind view will the url as the end-point.
    urlpatterns = [
         url(r'^admin/', admin.site.urls),
         url(r'^hello/$', hello), ]
     Run the server python manage.py runserver Congrats ! Our hello world server is ready.

    Rapid Development

    • Django was designed with the intention to make a framework which takes less time to build the web application. The project implementation phase is a very time taken but Django creates it rapidly.

    Secure

    • Django takes security seriously and helps developers to avoid many common security mistakes, such as SQL injection, cross-site scripting, cross-site request forgery etc. Its user authentication system provides a secure way to manage user accounts and passwords.

    Scalable

    • Django is scalable in nature and has an ability to quickly and flexibly switch from small to large scale application project.
    Fully Loaded
    • Django includes various helping task modules and libraries which can be used to handle common Web development tasks. Django takes care of user authentication, content administration, sitemaps, RSS feeds etc.

    Versatile

    • Django is versatile in nature which allows it to build applications for different-different domains. Nowadays, Companies are using Django to build various types of applications like content management systems, social networks sites or scientific computing platforms etc.

    Open Source

    • Django is an open source web application framework. It is publicly available without cost. It can be downloaded with source code from the public repository. Open source reduces the total cost of the application development.

    Vast and supported Community

    • Django is one of the most popular web frameworks. It has widely supportive community and channels to share and connect.

    Comments

    Popular Posts