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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.654s · 25.5MB
glibcpy 3.10–3.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>
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`.
fixEnsure 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.
fixAdd `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.
fixWhen 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.