Live a Haml life with Django1.8 + Jinja2 + hamlish-jinja (Python3)

Ham with Django has HamlPy, but Python3 probably doesn't support it. In fact, Python3 doesn't seem to have many options, and I thought that hamlish-jinja, which is rather dead, is a good idea.

hamlish-jinja is just a haml-ish syntax, not Haml, but I felt that I could be quite happy with Pythonic. I think the learning cost is almost 0! https://github.com/Pitmairen/hamlish-jinja

Well, in order to do that, I have to use Jinja2 for the template engine, but well, it's a strange story to change for that, but I like Jinja2 purely, not for Haml, I change it by saying that I like shrines and temples.

What I did to use the following.

Use Jinja2

http://qiita.com/ryu22e/items/e50f8a3fbd6fe836c1b4 The setting method around the template has changed considerably since 1.8! !! !! !! Also, it seems that Jinja2 can be easily introduced! !! !! !! !! However, it's quite difficult to do small things, so after all, as usual, pip django-jinja can make it a lot easier. The setting method is also detailed. With match_regex, you can switch which backend to use depending on the URL. So I think this is fine. http://niwinz.github.io/django-jinja/

hamlish-jinja put

pip install hamlish-jinja

So my setting.py looks like this.

from django_jinja.builtins import DEFAULT_EXTENSIONS
from hamlish_jinja import HamlishExtension

TEMPLATES = [ 
    {   
        "BACKEND": "django_jinja.backend.Jinja2",
        'DIRS': [], 
        'APP_DIRS': True,
        'OPTIONS': {
            'match_regex': r"^(?!admin/).*",
            'match_extension': ".haml",
            'extensions': DEFAULT_EXTENSIONS + [HamlishExtension]
        },  
    },  
    {   
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [], 
        '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, if you put XXX.haml in your usual template directory and specify XXX.haml in view, it will work normally.

%html
  %head
    %meta charset='utf-8'
    %meta name='viewport' content='width=device-width'
    %meta name='format-detection' content='telephone=no'
    %meta name='description' content={{description}}
    %meta name='keywords' content={{keywords}}
    %title << {{title}}
    %script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js' 

How nice.

However, hamlish-jinja alone does not have any mechanism to compile it into a jinja template first, so it is probably hard to put it into production as it is. So I decided to use the cache mechanism of jinja2 itself. If you cache it with jinja's bytecode, it doesn't matter what haml looks like. Should be.

Now this.

from jinja2 import FileSystemBytecodeCache
...
...
TEMPLATES = [
    {
        "BACKEND": "django_jinja.backend.Jinja2",
        'DIRS': [],            
        'APP_DIRS': True,      
        'OPTIONS': {           
            'match_regex': r"^(?!admin/).*",
            'match_extension': ".haml", 
            'extensions': DEFAULT_EXTENSIONS + [HamlishExtension],
            'bytecode_cache': FileSystemBytecodeCache(directory=os.path.join(BASE_DIR, 'project/cache'), pattern='%s.cache'),
        },
    },
 ...

Files will accumulate in the specified cache directory as appropriate. It's good because the time stamp of the file is updated only when haml is updated.

That's it.

Recommended Posts

Live a Haml life with Django1.8 + Jinja2 + hamlish-jinja (Python3)
Try to draw a life curve with python
[Python] Build a Django development environment with Docker
A simple to-do list created with Python + Django
Quickly build a Python Django environment with IntelliJ
A memo about building a Django (Python) application with Docker
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
Create a homepage with django
Getting Started with Python Django (4)
Make a fortune with Python
Getting Started with Python Django (6)
Getting Started with Python Django (5)
Recommendations for django, wagtail ~ Why develop a website with python ~
[Python] Create a screen for HTTP status code 403/404/500 with Django
[Python] What is a with statement?
Solve ABC163 A ~ C with Python
Operate a receipt printer with python
If you know Python, you can make a web application with Django
Save the result of the life game as a gif with python
Let's make a GUI with python.
Solve ABC166 A ~ D with Python
Deploy a Django application with Docker
Create a virtual environment with Python!
I made a fortune with Python.
Building a virtual environment with Python 3
Solve ABC168 A ~ C with Python
Make a recommender system with python
Life game with Python! (Conway's Game of Life)
Run python3 Django1.9 with mod_wsgi (deploy)
Build a web application with Django
Make a scraping app with Python + Django + AWS and change jobs
Solve ABC162 A ~ C with Python
Make a filter with a django template
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Let's make a graph with python! !!
[Python] Inherit a class with class variables
I made a daemon with Python
Write a batch script with Python3.5 ~
Create a file uploader with Django
[Pyenv] Building a python environment with ubuntu 16.04
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Create a Python function decorator with Class
[Python] A program that creates stairs with #
A simple RSS reader made with Django
Building a Python3 environment with Amazon Linux2
Let's make a shiritori game with Python
Install Python as a Framework with pyenv
Build a blockchain with Python ① Create a class
Add a Python data source with Redash
Create a dummy image with Python + PIL.
I made a character counter with Python
[Python] Drawing a swirl pattern with turtle
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
A memo with Python2.7 and Python3 on CentOS
Building a Python 3.6 environment with Windows + PowerShell
Map rent information on a map with python
Search the maze with the python A * algorithm