Registry / web-framework / pyramid-jinja2

pyramid-jinja2

JSON →
library2.10.1pypypi✓ verified 26d ago

Pyramid Jinja2 is a set of bindings that integrate the Jinja2 templating system with the Pyramid web framework. Currently at version 2.10.1, it is actively maintained by the Pylons Project, offering robust template rendering capabilities for Pyramid applications.

pip install pyramid-jinja2
INSTALL
IMPORT
SIG · PYRAMID-JINJA2
P
pyramid-jinja2
web-frameworkpythonv2.10.1
Install
3.0s avg
Import
622ms
Disk
28MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.10.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.654s · 25.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.0s · import 0.590s · 26MB
28MB installed
● package 28MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Configurator.include('pyramid_jinja2')
from pyramid.config import Configurator config = Configurator() config.include('pyramid_jinja2')
Activates the Jinja2 renderer and associated configurator directives.
add_jinja2_renderer
config.add_jinja2_renderer('.html', settings_prefix='jinja2.')
Used to register custom renderers with different file extensions or settings. Available after `config.include('pyramid_jinja2')`.
add_jinja2_search_path
config.add_jinja2_search_path('myapp:templates')
Adds a directory or asset specification to the Jinja2 environment's search path for templates.
get_jinja2_environment
env = config.get_jinja2_environment()
Retrieves the configured Jinja2 Environment object for direct manipulation (e.g., adding globals/filters dynamically). Requires `config.commit()` or deferred action for full configuration to apply.

This quickstart demonstrates a basic Pyramid application using `pyramid_jinja2` to render templates. It sets up two routes with corresponding views, each rendering a different Jinja2 template. The `config.include('pyramid_jinja2')` line is crucial for activating the Jinja2 renderer, and `jinja2.directories` is configured to locate templates.

from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Response from pyramid.view import view_config # Assuming a 'templates' directory exists with 'home.jinja2' in the same package # e.g., myapp/templates/home.jinja2 @view_config(route_name='home', renderer='home.jinja2') def home_view(request): return {'project': 'pyramid_jinja2 example', 'name': request.matchdict.get('name', 'World')} @view_config(route_name='hello', renderer='templates/hello.jinja2') def hello_view(request): return {'name': request.matchdict.get('name', 'Guest')} if __name__ == '__main__': with Configurator() as config: config.include('pyramid_jinja2') config.add_settings({'jinja2.directories': 'myapp:templates'}) # Or pass to Configurator(settings=...) config.add_route('home', '/') config.add_route('hello', '/hello/{name}') config.scan('.') # Scans for @view_config decorators app = config.make_wsgi_app() server = make_server('0.0.0.0', 6543, app) print('Serving Pyramid Jinja2 app on http://0.0.0.0:6543') print("Try: http://localhost:6543/ and http://localhost:6543/hello/Alice") server.serve_forever() # Example template (myapp/templates/home.jinja2): # <h1>Welcome to {{ project }}!</h1> # <p>Hello, {{ name }}!</p> # Example template (myapp/templates/hello.jinja2): # <h1>Greetings!</h1> # <p>Hello, {{ name }} from a different template!</p>
Debug
Known issues
breakingPython 2.x and older Python 3.x versions are no longer supported. Version 2.7 dropped Python 2.6 and 3.2, Version 2.8 dropped Python 3.3, and Version 2.9 dropped Python 3.6. The current version requires Python >=3.7.0.
fix
Upgrade your Python environment to 3.7 or newer before upgrading `pyramid_jinja2`.
affects: 2.7+
breakingAs of `pyramid_jinja2` 2.3, using `pyramid_jinja2.renderer_factory` now explicitly requires `pyramid_jinja2` to be included in the Configurator (via `config.include('pyramid_jinja2')` or `pyramid.includes` setting). Previously, it could be used without explicit inclusion.
fix
Ensure `config.include('pyramid_jinja2')` is present in your application's `__init__.py` or that `pyramid_jinja2` is listed in `pyramid.includes` in your .ini file.
affects: 2.3+
gotchaThe default behavior of `jinja2.bytecode_caching` changed in version 1.10 from `true` to `false`. Additionally, the automatic `atexit` callback for clearing bytecode cache files was removed.
fix
If you relied on bytecode caching for performance, explicitly set `jinja2.bytecode_caching = true` in your Pyramid settings. You must also implement a manual cleanup strategy (e.g., a cron job) for stale cache files if using filesystem caching.
affects: 1.10+
deprecatedThe `model_url` filter was deprecated in version 2.7 in favor of `resource_url` to align with Pyramid's vocabulary changes.
fix
Update your Jinja2 templates to use `resource_url` instead of `model_url` for generating URLs related to resources.
affects: 2.7+
gotchaFor custom Jinja2 filters, Jinja2 >= 3.0 deprecated `@jinja2.contextfilter` in favor of `@jinja2.pass_context`. `pyramid_jinja2` version 2.9.2 and later supports both, but newer Jinja2 versions will warn or break with `contextfilter` usage.
fix
If you define custom Jinja2 filters, use `@jinja2.pass_context` instead of `@jinja2.contextfilter` for compatibility with Jinja2 3.0 and newer.
affects: Jinja2 3.0+
Errors
Common errors & fixes
jinja2.exceptions.TemplateNotFound: your_template_name.jinja2
The Jinja2 environment cannot locate the specified template file, usually because the template directory is not correctly configured in Pyramid's `add_jinja2_search_path`.
fix
Ensure you have registered your template directory in your `__init__.py` or configuration. For example, if 'templates' is a directory inside 'your_package':
```python
# In your_package/__init__.py
config.include('pyramid_jinja2')
config.add_jinja2_renderer('.jinja2') # or other extension
config.add_jinja2_search_path('your_package:templates')
```
pyramid.exceptions.ConfigurationError: Renderer for .jinja2 extension is not registered.
Pyramid does not know how to render files with the '.jinja2' (or your chosen) extension because the `pyramid_jinja2` renderer has not been properly included or added to the configuration.
fix
Add `config.include('pyramid_jinja2')` and `config.add_jinja2_renderer('.jinja2')` (or your desired extension) in your Pyramid application's `__init__.py` or equivalent configuration setup:
```python
# In your_package/__init__.py
config.include('pyramid_jinja2')
config.add_jinja2_renderer('.jinja2')
```
TypeError: render_to_response() missing 1 required positional argument: 'request'
The `render_to_response` utility function requires the current Pyramid `request` object to be passed as an argument to correctly resolve paths, provide globals, and prepare the response.
fix
When calling `render_to_response`, always pass the `request` object from your view:
```python
from pyramid.view import view_config
from pyramid_jinja2 import render_to_response

@view_config(route_name='home') # No 'renderer' arg here if using render_to_response directly
def home_view(request):
    # ... your view logic ...
    return render_to_response(
        'your_package:templates/mytemplate.jinja2',
        {'page_title': 'Welcome'},
        request=request # Crucial: pass the request object
    )
```
Upgrade
Version history
2.10.1latest on PyPI · released Feb 8, 2024
Audit
Dependencies
pyramidrequiredCore web framework dependency.
jinja2requiredCore templating engine dependency.
Agent activity
17 hits · last 30 days
node
6
Resources
pyramid-jinja2 — pip install pyramid-jinja2 · libregistry