Skip to content

Instantly share code, notes, and snippets.

@poojansmobio
Last active Sep 4, 2020
Embed
What would you like to do?
<!doctype html>
<html>
<head>
<title>{{ site_title }}</title>
</head>
<body>
<h2> {{ indextitle }} </h2>
</body>
</html>
from django.urls import path
from .views import *
from . import views
urlpatterns = [
path('',IndexView.as_view(),name="index"),
]
from django.views.generic import TemplateView
from .models import *
"""
Base View
"""
class Base(TemplateView):
"""
This is Base class
The functions and variables used
in this class can be used whichever
class inherits this Base class.
"""
def get_global_settings(self):
"""
Function to get values from database
"""
self.global_setting = tbl_setting_master.objects.all().values('key','value')
def get_context_data(self, **kwargs):
context = super(Base,self).get_context_data(**kwargs) #creating context variable
setting = self.get_global_settings() #calling function
main_title = next(item for item in setting if item["key"] == "main_title") #value to variables.
context['site_title'] = main_title #Passing variable to context
return context
"""
Index View
"""
class IndexView(Base):
"""
This class is inherited from Base class so the
context variable of Base class will be defined directly added.
"""
template_name = "index.html"
"""
We use more functions for more operations
"""
def indexmethod(self):
self.variable_of_this_func = "IndexTitle"
return self.variable_of_this_func
def get_context_data(self, **kwargs):
"""
This variables are of IndexView which is for index only.
"""
context = super(IndexView,self).get_context_data(**kwargs)
index_title = self.indexmethod()
context['indextitle'] = index_title
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment