Skip to main content

Introduction: What Is Portal Context in Frappe?

In Frappe Framework v15, portal context refers to the data dictionary passed from Python to a portal web page at runtime. This context controls what data is available to the template, how the page behaves, and whether access restrictions apply.
Portal context is the backbone of dynamic portal pages and enables personalization, permission checks, and conditional rendering for logged-in users.

What Does Portal Context Control?

  • Portal context can control:
  • Page titles and metadata
  • Logged-in user data
  • Access permissions
  • Data fetched from DocTypes
  • Visibility of sections and actions

All of this is handled server-side using standard Frappe APIs.

How Portal Context Works in Frappe v15

Answer:
When a portal page is requested, Frappe executes the associated Python file (if present) and injects a context object into the rendering pipeline. This object is then available inside the Jinja template.

The context lifecycle is:

  1. Request hits portal route
  2. Python controller executes
  3. Context dictionary is populated
  4. Template renders using context values

Where Portal Context Is Defined

Portal context is defined in a Python file with the same name as the page.

Example file structure

www/
├── orders/
│ ├── index.html
│ ├── index.py

The index.py file controls the portal context.

Basic Portal Context Example

Python (index.py)
def get_context(context):
context.title = "My Orders"
Template (index.html)
<h1>{{ title }}</h1>

This renders a dynamic page title using portal context.

Making a Portal Page Login-Protected

To restrict access to logged-in users only:

def get_context(context):
context.login_required = True

Result:

  • Unauthenticated users are redirected to login
  • Page data is protected automatically

This behavior is enforced by Frappe v15 core logic.

Accessing Logged-In User Data

You can safely access user details inside portal context.

Example

import frappe
def get_context(context):
context.user = frappe.session.user
In the template:
<p>Welcome, {{ user }}</p>

Fetching DocType Data in Portal Context

Portal context can query DocTypes using standard ORM calls.

Example

import frappe
def get_context(context):
context.orders = frappe.get_all(
"Sales Order",
filters={"customer": frappe.session.user},
fields=["name", "transaction_date", "status"]
)

This data becomes available to the portal template.

Conditional Rendering Using Context

Context values can control visibility logic.

Python

def get_context(context):
context.show_summary = True

Jinja

{% if show_summary %}
<div>Order Summary</div>
{% endif %}

Portal Context vs Desk Context

Aspect Portal Context Desk Context
Audience External users Internal users
Security Login-aware Role-based
Rendering Jinja templates Vue / JS
File location /www App modules

Portal context is strictly for website and portal pages.

Best Practices for Portal Context

  • Keep logic minimal and readable
  • Avoid heavy database queries
  • Always enforce login_required for sensitive data
  • Use clear, descriptive context keys
  • Validate user permissions explicitly when required

Common Mistakes to Avoid

  • Writing business logic in templates
  • Skipping permission checks
  • Overloading context with unnecessary data
  • Mixing Desk APIs into portal code

Real-World Use Case

Customer Self-Service Portal

  • Orders list
  • Invoice downloads
  • Shipment tracking

Portal context dynamically fetches customer-specific data while enforcing login security.

Troubleshooting Portal Context Issues

Page loads but data is missing

  • Check context variable names
  • Verify Python file is executed

Login redirect not working

  • Confirm login_required = True

Permission error

  • Validate DocType permissions for portal user

Technical Scope & Compatibility

Framework: Frappe Framework v15
Modules: Website, Portal
Applies to: ERPNext portals, custom Frappe apps
Not applicable: Desk Workspaces, Reports

Cross-References

Official Portal Context Docs

https://docs.frappe.io/framework/user/en/guides/portal-development/context

Portal Pages

https://docs.frappe.io/framework/user/en/guides/portal-development/adding-pages

Frappe v15 Source Code

https://github.com/frappe/frappe/tree/version-15

Industry Relevance

  • ERPNext customer portals
  • Supplier dashboards
  • Member portals
  • SaaS self-service applications

Conclusion

Portal context in Frappe Framework v15 is the foundation for building secure, dynamic, and user-aware portal pages. By leveraging the get_context method correctly, developers can deliver personalized experiences while maintaining performance and security.
When used as designed, portal context enables scalable portal development without custom routing or client-side complexity.

Rating: 0 / 5 (0 votes)