Custom Template Tags and Filters in Django

Reverie

Custom Template Tags/Filters are necessary when there is a need to modify the object received from views context, such that it suits the needs of rendering them in the template. Django’s out-of-the-box tags/filters might not always suffice. The custom tag/filter can be registered in Django’s tags or filters Library and used in the templates. Let’s learn something about writing our own tags and filters. Let me start off from the beginning:

  • Create a new Django Project:

django-admin startproject <proj_name>

  • Create an app in the project:

python manage.py startapp temptags

  • Add the App to INSTALLED_APPS in settings.py

  • Create a templatetag folder inside the app folder.

  • Create a __init__.py file to treat the folder as python package.

  • Create a ‘templates’ folder to add the html files

  • urls.py

from django.urls import pathfrom . import views

urlpatterns = [path('',views.sampleTemptag),]

  • views.py

from django.shortcuts import render

def sampleTemptag(request):
    context = {
        "name": "My name is Agnes.000123",
        "id": "89752358-1234",
        "fullname": "Agnes George Sequera"}
    return render(request,'main.html',context)

View original post 427 more words

Advertisement

JavaScript Quick Revision – 3

Topics Covered JS EngineV8 Browser ArchitectureConcurrency Model in JSHigher Order FunctionsMap, Filter and Reduce JS Engine Javascript Runtime Environment is a container which has everything required to run a JS code. Every Browser has a JS Runtime Environment. Node.js has its own runtime environment. Javascript Runtime Environment commonly has the following components: Javascript EngineAPIs that … Continue reading JavaScript Quick Revision – 3

JavaScript Quick Revision – 2

Topics Covered: ClosureSetTimeoutAll About functions: Function StatementExpression & DeclarationAnonymous FunctionsNamed function expressionParameters & ArgumentsFirst class functionsArrow functionsCallback FunctionsEvent ListenersEvent Loop Closure Closures are functions bundled with (along with) its lexical scope. When Functions are returned, they still maintain their Lexical scope. function x() { var a = 10; //same if let return function y() { … Continue reading JavaScript Quick Revision – 2

React Native – Introduction

React Native is an open-source UI framework used to develop mobile applications. It provides the capability to use the React framework along with native platform capabilities. React Native can be used for building both iOS and Android apps. However if you directly have to talk to the native APIs you will have to go with … Continue reading React Native – Introduction

JavaScript Quick Revision-1

Topics Execution ContextCall StackHoistingUndefined vs not definedwindow objectLoosely/weakly typedLexical EnvironmentScope ChainTemporal Dead zone (let & const)Common Error TypesBlock ScopeShadowing Execution Context and Call Stack: Execution context is made of two components: Memory or Variable Environment: Stores variables as Key-Value pairsCode or Thread execution: Executes code line by line There are two phases of JS program … Continue reading JavaScript Quick Revision-1

Custom Template Tags and Filters in Django

Custom Template Tags/Filters are necessary when there is a need to modify the object received from views context, such that it suits the needs of rendering them in the template. Django's out-of-the-box tags/filters might not always suffice. The custom tag/filter can be registered in Django's tags or filters Library and used in the templates. Let's … Continue reading Custom Template Tags and Filters in Django

Apache Kafka – Basics

Apache Kafka is a distributed real-time data streaming platform. Processing Payments, Tracking and Monitoring real-time, continuous analytics of sensor data are some examples of Event Steaming. Kafka consists of servers and clients communicating through a high performance TCP network protocol. The data is organized into Topics making it efficient and easy for retrieval. It is … Continue reading Apache Kafka – Basics

Docker Compose – Containerizing a Django Application

In the previous blog: Docker – Deep Dive, we created a Dockerfile with a set of instructions, started a service and create a container. Actual Projects are usually much more complex and require more than one services at a time running on multiple containers. Docker Compose handles exactly this. Docker Compose is a tool to configure … Continue reading Docker Compose – Containerizing a Django Application

Docker – Deep Dive

Docker – Introduction covered the evolution, the need for Docker and the reason why it is widely used. This post will be a deep dive into the concepts of Docker and it's practical use cases. This post covers everything required to create an image and run an application using docker. The OS I have chosen … Continue reading Docker – Deep Dive

Django Custom Middleware

A Custom middleware can validate each http request for the set of instructions given in the middleware, before sending back a response. Let us see how we can write a custom Middleware. As an example, we will be implementing a custom Authentication Middleware here. Django's out-of-the box Authentication middleware only authenticates requests if the project … Continue reading Django Custom Middleware