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

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

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

Building a Twitter Clone with Django REST Framework – III

Previous posts: Building a Twitter Clone with Django REST Framework – I Building a Twitter Clone with Django REST Framework – II In this post, let's put some of the APIs to use: Create a new user using the 'signup' API end point Login to the application Login with wrong credentials Get all the followers Creating a … Continue reading Building a Twitter Clone with Django REST Framework – III

Building a Twitter Clone with Django REST Framework – II

Previous post: Building a Twitter Clone with Django REST Framework – I User Authentication We will perform Authentication using JWT or JSON Web Token. JWT is a secret token that is generated and sent to the browser when a user successfully logs in with his credentials. Subsequently, whenever the user accesses any link, the token is … Continue reading Building a Twitter Clone with Django REST Framework – II

Building a Twitter Clone with Django REST Framework – I

In this blog-series, I will be sharing my experience building an App with features similar to Twitter, using Django REST. There will be a code snippet for each step. You can find the entire code on GitHub too: https://github.com/shilpavijay/TwitterCloneThe approach I have taken is TDD - Test Driven Development. There will be a test case … Continue reading Building a Twitter Clone with Django REST Framework – I

Python 3.7 – Cool New features

Welcome back, to Python! It's time to discuss the cool new features shipped with Python 3.7. Here are some of my favorites: Data Classes Built-in breakpoint Typing module Importing Data files Data Classes What is it? A new decorator: @dataclass What's new? It eases writing special methods in the class, like __init__(), __repr__(), and __eq__(. They are added … Continue reading Python 3.7 – Cool New features