Django Python Framework for web application

Django Tutorial using Visual Studio

Django is a high-level Web development framework specially designed for Python developer, it provides a ready to set of clean templates to start rapid web development, Django is free and open source, so you can customise your web app as per need. Read more about Django python Framework

Fortunately in visual studio 2017 (and above) we get integrated Django python Framework template to quickly start with!

Let’s open visual studio, select Python => Django Web Project

python django framework

Once you create the project, you will get following ready to use Django web template included in the project, so before you run the project let’s understand the folder structure and files in the project

Python Django Framework for Web Project

If you have any web development experience in any other languages, that will be definitely an advantage, if not, nothing to worry, you can learn easily now!

python django framework

Remember all view(page) you create, must be registered (defined) in file as shown below, Define all Django views in app/ file file,

from django.shortcuts import render
from django.http import HttpRequest
from django.template import RequestContext
from datetime import datetime
def home(request):
    """Renders the home page."""
    assert isinstance(request, HttpRequest)
    return render(
            'title':'Home Page',
Things to learn about Django Python Views / Layout
  1. Notice how to add a master page / layout page reference in any view (page), we have created a new page, where we want to add "layout.html" as a master page. use "extends" to inherit the master page layout

        {% extends "app/layout.html" %}
  2. In python view we can add any plain non-python content within a block {% block content %}, Whenever you start a block make sure you close the block with {% endblock %} statement,This is actually content placeholder defined in master page. Here is how to add content in any python view

    {% block content %}
    <h2>{{ variableName1 }}.</h2>
    <h3>{{ variableName2 }}</h3>
    <p>any plain content can be written here, content like text, html, images everything you need to make your webpage meaningful.</p>
    {% endblock %}
  3. How to include any partial view in a view or in master page

    {% include 'app/loginpartial.html' %}
  4. How to add any JavaScript file or CSS file reference in your python project, notice {% static 'file path' %}

    <link rel="stylesheet" type="text/css" href="{% static 'app/content/site.css' %}" />
    <script src="{% static 'app/scripts/modernizr-2.6.2.js' %}"></script>
  5. This is how you can define content place holder in layout page or master page, just define it empty.

    {% block content %}{% endblock %}
  6. Like content place holder, we can define optional script placeholder in python layout page

    {% block scripts %}{% endblock %} : Application configuration or setting details class (in root of Django Web Project) contains all application settings like list of installed app, middleware classes, database connection details, static folder path, time zone, default language etc.

But before you configure database connection details, make sure you have installed right database package in your python project environment.

Python Django Development with Visual Studio
Python programming examples | Join Python Course