[PYTHON] Make a filter with a django template

Preface

I'm making a web application using mongodb and django in Python, but I've come across a situation where I want to display the id to identify the item obtained from mongodb that repeats in the template.

manner

I thought about the method of writing {{item._id}} in the template, but since ʻitem.id is " id": {"$ oid": "54cac41fe4b0b3c7e59cff77"} `, It cannot be displayed as it is.

Therefore, I decided to display it by adding a filter to the template.

Directory structure

Make the directory structure as follows. Create a directory called templatetags.

|-view.py
|-templatetags/
| |- __init__.py
| |- filter.py
|
|-template/
  |- template.html

Contents of each file

It seems that __init__.py needs to be written by promise in order to recognize this directory (templatetags) as a package.

__init__.py


#OK in the sky

Describe the contents of the filter in filter.py. Here, the value of _id is output as a character string.

filter.py


from django import template
register = template.Library()
 
@register.filter("mongo_id")
def mongo_id(value):
    return str(value['_id'])

template.html looks like this, and it seems that you can write it in the form of{{object | filter}}. You need to load the package you want to use.

template.html


{% load filter %}
{% for item in items %}
  <input type="hidden" name="id" value="{{item|mongo_id}}"/>
{% endfor %}

reference

-Django Template Language

Recommended Posts

Make a filter with a django template
[Django 2.2] Add a New badge to new posts with a date using a template filter
Create a homepage with django
[Django] Make a pull-down menu
Make a fortune with Python
Make a fire with kdeplot
Make the model a string on a Django HTML template
Let's make a GUI with python.
Make a sound with Jupyter notebook
I want to make a blog editor with django admin
Deploy a Django application with Docker
Let's make a breakout with wxPython
Django # 2 (template)
Django Tips-Create a ranking site with Django-
Make a recommender system with python
Build a web application with Django
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Make a model iterator with PySide
Make a nice graph with plotly
Create a file uploader with Django
Make a scraping app with Python + Django + AWS and change jobs
A simple RSS reader made with Django
Let's make a shiritori game with Python
Make a video player with PySimpleGUI + OpenCV
Make a rare gacha simulator with Flask
Make a Notebook Pipeline with Kedro + Papermill
Make a partially zoomed figure with matplotlib
Make a drawing quiz with kivy + PyTorch
Let's make a voice slowly with Python
Creating a login screen with Django allauth
Make a cascade classifier with google colaboratory
Let's make a simple language with PLY 1
Make a logic circuit with a perceptron (multilayer perceptron)
Make a Yes No Popup with Kivy
A note on enabling PostgreSQL with Django
Make a wash-drying timer with a Raspberry Pi
Make a GIF animation with folder monitoring
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Write a short if-else for Django Template
I made a WEB application with Django
Django HTML Template # 2
Internationalization with django
Django Template notes
Django HTML template
Django Template Tips
CRUD with Django
Django filter summary
A memorandum to make WebDAV only with nginx
Investment quest: Make a system trade with pyhton (2)
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
Make a monitoring device with an infrared sensor
How to develop a cart app with Django
Start Django in a virtual environment with Pipenv
Investment quest: Make a system trade with pyhton (1)
How to make a dictionary with a hierarchical structure.