Rendering Static HTML page using Django - onlinetutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we will learn how to render static HTML pages in a Django application.
Python 3.7, 3.8
Django 3.1
HTML 5
The first step of beginning any python project is creating a virtual environment. Follow this tutorial to set up and activate a virtualenvand then install Django.
virtualenv env
source env/bin/activate
pip install Django
Start a Django project (here, project name:django_html) and create an app named home. Check this tutorial for more details.
django-admin startproject django_html
cd django_html/
python manage.py startapp home
Django uses templates to generate HTML dynamically. A template contains the static part as well some static syntax for displaying the dynamic content of the desired HTML page. Django has the Django template language for its own template system.
Create a directory named templates inside the newly created home apps directory.
By convention, Django’s template engine looks for a ‘templates’ subdirectory in each of the INSTALLED_APPS.
mkdir home/templates
Inside the templates dir, create a file named index.html with the following content.
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Home Page</title>
</head>
<body>
    <h1>Home Page</h1>
    <p>This page is rendered as a Django Template</p>
    <h3>Happy Learning :)</h3>
</body>
</html>
Add the name of the newly created app home to the INSTALLED_APPS list in settings.py.
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home',
]
Have a look at the TEMPLATES settings in the settings.py file, which contains the configurations for the templates engines and describes how Django will load and render templates.
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]
BACKEND: Path to the default Django Template engine class.
DIRS: List of directories where the engine should look for source files, in given search order.
APP_DIRS: If True, the engine will look for templates inside installed applications.
OPTIONS: contains backend-specific settings.
We do not have to make any changes in these settings. As we have listed our app in the INSTALLED_APPS list, other things are taken care of by Django’s template engine.
Next, create a view in home/views.py for the template.
from django.shortcuts import render

def index(request):
    return render(request, 'index.html')
The index() method renders the HTML page. This is made possible using the render helper function from the django.shortcuts package.
Here, the render() function takes the request object and the template_name as the arguments and returns an HttpResponse object with the rendered text.
Create a file urls.py inside app directory home/ with the following content.
from django.urls import path
from . import views
urlpatterns = [
    path('', views.index),
]
Next, create a URL pattern for this app in the URL patterns for our project, ie. in the file django_html/urls.py.
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('home.urls')),
    path('admin/', admin.site.urls),
]

Now, run the application using
python manage.py runserver
Navigate to http://localhost:8000/ and you can see the HTML page create being rendered.
Django Templates Documentation
Django Template Language
Django Shortcut functions
Django Official Tutorial
Happy Learning 🙂

Python Django Helloworld Example
AngularJs Directive Example Tutorials
How to setup Python VirtualEnv
How to Create own Spring Boot Error Page
Java Static Variable Method Block Class Example
Java Main Method – public static void main(String args[])
How to push docker image to docker hub ?
Default Static methods in Interface Java 8
JQuery Events Example Tutorials
Different ways to use Lambdas in Python
Spring MVC Tiles Example (Apache Tiles)
Java Swing JOptionPane Html Content Example
Python Selenium Automate the Login Form
Implementing Stack in Python
Angularjs Custom Filter Example

Python Django Helloworld Example
AngularJs Directive Example Tutorials
How to setup Python VirtualEnv
How to Create own Spring Boot Error Page
Java Static Variable Method Block Class Example
Java Main Method – public static void main(String args[])
How to push docker image to docker hub ?
Default Static methods in Interface Java 8
JQuery Events Example Tutorials
Different ways to use Lambdas in Python
Spring MVC Tiles Example (Apache Tiles)
Java Swing JOptionPane Html Content Example
Python Selenium Automate the Login Form
Implementing Stack in Python
Angularjs Custom Filter Example
 


Δ
 Python – Introduction
 Python – Features
 Python – Install on Windows
 Python – Modes of Program
 Python – Number System
 Python – Identifiers
 Python – Operators
 Python – Ternary Operator
 Python – Command Line Arguments
 Python – Keywords
 Python – Data Types
 Python – Upgrade Python PIP
 Python – Virtual Environment
 Pyhton – Type Casting
 Python – String to Int
 Python – Conditional Statements
 Python – if statement
 Python – *args and **kwargs
 Python – Date Formatting
 Python – Read input from keyboard
 Python – raw_input
 Python – List In Depth
 Python – List Comprehension
 Python – Set in Depth
 Python – Dictionary in Depth
 Python – Tuple in Depth
 Python – Stack Datastructure
 Python – Classes and Objects
 Python – Constructors
 Python – Object Introspection
 Python – Inheritance
 Python – Decorators
 Python – Serialization with Pickle
 Python – Exceptions Handling
 Python – User defined Exceptions
 Python – Multiprocessing
 Python – Default function parameters
 Python – Lambdas Functions
 Python – NumPy Library
 Python – MySQL Connector
 Python – MySQL Create Database
 Python – MySQL Read Data
 Python – MySQL Insert Data
 Python – MySQL Update Records
 Python – MySQL Delete Records
 Python – String Case Conversion
 Howto – Find biggest of 2 numbers
 Howto – Remove duplicates from List
 Howto – Convert any Number to Binary
 Howto  – Merge two Lists
 Howto – Merge two dicts
 Howto – Get Characters Count in a File
 Howto – Get Words Count in a File
 Howto – Remove Spaces from String
 Howto –  Read Env variables
 Howto – Read a text File
 Howto – Read a JSON File
 Howto –  Read Config.ini files
 Howto – Iterate Dictionary
 Howto – Convert List Of Objects to CSV
 Howto – Merge two dict in Python
 Howto – create Zip File
 Howto – Get OS info
 Howto – Get size of Directory
 Howto – Check whether a file exists
 Howto –  Remove key from dictionary
 Howto – Sort Objects
 Howto – Create or Delete Directories
 Howto – Read CSV File
 Howto – Create Python Iterable class
 Howto – Access for loop index
 Howto – Clear all elements from List
 Howto – Remove empty lists from a List
 Howto – Remove special characters from String
 Howto – Sort dictionary by key
 Howto – Filter a list