Skip to main content

rendering html page

As django is a web framework it needs a convenient way to generate HTML dynamically. To achieve this most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.
Django ships build-in backend for its own system which is known as Django template language (DTL). Django defines a standard API for loading and rendering templates regardless of the backend.

Let's start now
Now first we will create an app inside our project using following command. Please make sure virtual environment is enabled.

       
python manage.py startapp web
       
 

Now using above command we have create an app named as 'web', but django is not aware that any
app is created, so to make aware django of this app we need to make entry of this app in settings.py
in the tuple INSTALLED_APP
       
INSTALLED_APPS = [
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'Django.contrib.staticfiles',
   'web'
]

       
 

Our next step is to create a directory named as templates where our mange.py is located. So in this directory we will be storing all html templates.

Now we need to create html file which we will open as our home page in our browser. Let's create a file index.html in templates.

Now we need to tell django that we have kept our html template at this location for this we have to make in template dirs in settings.py as shown below




       
TEMPLATES = [
   {
       'BACKEND': 'django.template.backends.django.DjangoTemplates',
       'DIRS': ['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',
           ],
       },
   },
]
       
 

Now we need to create a view which will render this html page. Here is the code which we have to write in view

       
def home_page(request):
   return render(request, 'index.html')
       
 

So this is very basic function which will render index.html Now next is to bind is view to a url so that we can open it in browser using that endpoint. For this we need to go inside demoProject and there we will find a file known as urls.py here we need to import our function which we create and needed to map it to end point as shown in code below

       
from django.contrib import admin
from django.urls import path
from web.views import home_page
urlpatterns = [
   path('admin/', admin.site.urls),
   path('home/', home_page)
]
       
 

Now open command line and go to the place where manage.py is and run following command to run django application.

       
python manage.py runserver
   
 

Now open your browser and navigate to following end point http://127.0.0.1:8000/home/ 

Source code can be referred from here https://github.com/anshul217/django-basic-tutorial-1/

Comments