Registry / web-framework / zope-browser

zope-browser

JSON →
library4.0pypypi✓ verified 89d ago

zope.browser provides shared Zope Toolkit browser components, including base classes for browser views, browser resources, and helper utilities. It's an integral part of the Zope Toolkit, designed for building modular web applications with a strong emphasis on interfaces and component architecture. Version 4.0 is the current stable release, compatible with Python 3.9+.

pip install zope.browser
INSTALL
IMPORT
SIG · ZOPE-BROWSER
Z
zope-browser
web-frameworkpythonv4.0
Install
2.1s avg
Import
26ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v4.0 · 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.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.028s · 19.8MB
glibc
py 3.10–3.910 runs
installs and imports cleanly · install 2.1s · import 0.024s · 20MB
22MB installed
● package 22MB
Code
Verified usage

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

IBrowserView
✓ from zope.browser.interfaces import IBrowserView
BrowserView
✓ from zope.publisher.browser import BrowserView
✗ from zope.browser.publisher import BrowserView
While `zope.browser.publisher` exists, the canonical base class for browser views is now `zope.publisher.browser.BrowserView` since `zope.publisher` handles the core publishing logic.
Resource
✓ from zope.browser.resource import Resource

This example demonstrates how to define a basic `IBrowserView` and its implementation `MyBrowserView` which inherits from `zope.publisher.browser.BrowserView`. It shows how views typically access `self.context` (the object being viewed) and `self.request` (the HTTP request). The `if __name__ == '__main__':` block illustrates how a view is instantiated with dummy context and request objects for testing purposes, mimicking the behavior of the Zope publisher.

import zope.interface import zope.schema import zope.publisher.browser # 1. Define an interface for the content we want to view class IMyContent(zope.interface.Interface): """An interface for some content object.""" title = zope.schema.TextLine(title="Title") # 2. Define a simple content object that implements the interface @zope.interface.implementer(IMyContent) class MyContent: def __init__(self, title="Default Content"): # pragma: no cover self.title = title # 3. Define the browser view class MyBrowserView(zope.publisher.browser.BrowserView): """A simple browser view that uses context and request.""" def __call__(self): # self.context is the content object # self.request is the HTTP request return f"<h1>Hello from {self.context.title}!</h1>" def get_message(self): return f"View message for {self.context.title}" # Example of instantiating and using the view for testing/demonstration # In a real Zope application, this would be handled by the Zope publisher. if __name__ == "__main__": from zope.publisher.interfaces.http import IHTTPRequest from zope.interface.declarations import implementer @implementer(IHTTPRequest) class DummyRequest: # pragma: no cover # Minimal request object for instantiation pass content = MyContent(title="My Awesome Page") request = DummyRequest() view = MyBrowserView(context=content, request=request) print(view()) # Output: <h1>Hello from My Awesome Page!</h1> print(view.get_message()) # Output: View message for My Awesome Page
Debug
Known issues
breakingPython 2 to 3 migration: Version 4.0 of zope.browser is Python 3 only (requires Python >= 3.9). Earlier versions (e.g., 1.x, 2.x) were Python 2 compatible. Migrating from older Zope applications on Python 2 will require updating zope.browser and its dependencies, potentially involving code changes due to API shifts and Python 3 syntax.
fix
Upgrade your Python environment to 3.9+ and ensure all Zope Toolkit dependencies are updated to their Python 3 compatible versions. Review code for Python 2 specific idioms.
affects: <4.0
gotchaBrowser views rely on Zope's Component Architecture (zope.component) for lookup and registration. Unlike typical web framework views, they are not directly instantiated by URL routing but looked up based on context, request, and view name via `zope.component.getMultiAdapter`. Misconfigured or missing component registrations (e.g., in ZCML or programmatically) are a very common source of errors.
fix
Thoroughly understand zope.component registration mechanisms. Always verify that a view factory is correctly registered for the specific content interface, request interface, and view name you intend to use. For debugging, use `zope.component.showAdapters` or similar tools.
affects: All versions
gotchaViews inheriting from `zope.publisher.browser.BrowserView` (or its ancestors) expect `context` (the object being viewed) and `request` (the HTTP request) to be passed to their constructor. Incorrectly handling these, especially when manually instantiating a view outside the Zope publisher, will lead to `TypeError` or `AttributeError`.
fix
Ensure that `context` and `request` are always passed to the `BrowserView` constructor. If subclassing and overriding `__init__`, remember to call `super().__init__(context, request)` to ensure the base class properly sets these attributes.
affects: All versions
Errors
Common errors & fixes
zope.component.interfaces.ComponentLookupError: ('No matching adapter found for (...), None, ...')
The Zope Component Architecture could not find a suitable browser view registered for the given combination of context, request, and view name.
fix
Verify your ZCML or programmatic `zope.component.provideAdapter` registrations. Ensure an adapter is provided for the exact interfaces of your context and request, and the correct view name, implementing `IBrowserView`.
TypeError: __init__() missing 2 required positional arguments: 'context' and 'request'
You are attempting to directly instantiate a `BrowserView` (or a class inheriting from it) without providing the mandatory `context` and `request` arguments to its constructor.
fix
When manually instantiating a view (e.g., in tests), always pass both `context` and `request` objects: `MyView(context=my_content_object, request=my_request_object)`. In a live Zope application, the publisher handles this.
AttributeError: 'MyBrowserView' object has no attribute 'request'
This usually indicates that the `request` object (or `context`) was not correctly passed to the `BrowserView` constructor, or `super().__init__(context, request)` was not called in a subclass's `__init__` method.
fix
Ensure that your view class's `__init__` method (if overridden) explicitly calls `super().__init__(context, request)`. If not overridden, ensure that the `BrowserView` is instantiated with both arguments.
Upgrade
Version history
4.0latest on PyPI · released Sep 12, 2025
Audit
Dependencies
zope.interfacerequiredCore dependency for defining interfaces and adapting objects, fundamental to Zope's component architecture.
zope.locationrequiredProvides traversal mechanisms and location awareness for objects within a Zope application.
zope.publisherrequiredDefines the publishing mechanism and base classes for HTTP requests and responses, which browser views interact with.
zope.schemarequiredUsed for defining typed attributes in interfaces, often for form fields or data validation.
zope.securityrequiredProvides security mechanisms, including permission checking for views and other components.
Agent activity
46 hits · last 30 days
node
38
Amazon
1
OpenAI (training)
1
Resources
zope-browser — pip install zope-browser · libregistry