Frappe Jinja TemplatesJinja

How to Fetch User Data in Frappe & ERPNext Jinja Templates

Fetch Session User and Modified

Learn how to fetch current session user details, modified by user, and other employee data using frappe.db.get_value and frappe.user in Frappe Jinja templates.

In the Frappe framework, Jinja templating is a powerful feature used for creating dynamic content in Print Formats, Custom HTML fields, and Web Pages. A common requirement is to display information related to the current user or the users who have interacted with a document. This collection of snippets provides a practical guide to fetching various user details directly within your Jinja templates.

These examples leverage built-in Frappe API methods that are safely exposed to the Jinja rendering context, allowing you to perform simple database queries and access session information without writing complex server scripts.

jinja
1// Get the ID of the current session user
2{{ frappe.user }}
3
4// Get the full name of the user who last modified the document
5{{ frappe.get_fullname(doc.modified_by) or "" }}
6
7// Fetch the designation of the modifying user from the Employee DocType
8{{ frappe.db.get_value("Employee", { "user_id": doc.modified_by }, "designation") or "" }}
9
10// Fetch a user's full name by their ID (assuming 'manager' is a variable)
11{{ frappe.db.get_value("User",{"name":manager},"full_name") }}
12
13// Fetch the employee_name of the current session user
14{{ frappe.db.get_value("Employee",{"user_id":frappe.session.user},"employee_name") }}
15

Understanding This Code

What It Does

This collection of Jinja snippets demonstrates various methods to retrieve user-related information within a Frappe Framework template. It covers fetching the current session user, the user who last modified a document, and related details like full name or designation from linked DocTypes.

When To Use

Use these snippets in any Frappe context that supports Jinja templating. This is most common in custom Print Formats, custom HTML fields on a Form, or in web pages to display user-specific information dynamically without needing a separate server script.

Key Concepts

Important ideas to understand in this code

Session User Variables

`frappe.user` and `frappe.session.user` are context variables available in Jinja templates that provide the User ID (typically the email address) of the currently logged-in user.

Learn more

The `doc` Object

In the context of a Print Format or Form, the `doc` object represents the current document being processed. You can access its fields, like `doc.modified_by` or `doc.owner`, to get data directly from the document.

Learn more

frappe.db.get_value

A whitelisted Frappe API method that allows you to safely fetch a single value from the database directly within a Jinja template. It requires the DocType, filters, and the field name you want to retrieve.

Learn more

Step-by-Step Tutorial

Follow along to understand how this code works

1

Access Your Print Format

Navigate to the Print Format list and select the one you wish to customize, for example, for the 'Sales Invoice' DocType. Ensure you are using a Custom Print Format by checking the 'Custom Format' checkbox and selecting 'Jinja' as the templating engine.

bash
// Navigate via the Awesomebar: 'Print Format List'
Next Step
2

Insert Jinja Code to Display Creator's Name

In the 'HTML' field of your Print Format, find a suitable location to display the full name of the user who created the document. We'll use `frappe.get_fullname` and the `doc.owner` field.

html
<div>
    <strong>Created By:</strong>
    {{ frappe.get_fullname(doc.owner) }}
</div>
Next Step
3

Fetch a Related Value from Another DocType

Now, let's fetch the designation of the user who last modified the document. We'll query the 'Employee' DocType using `frappe.db.get_value`, filtering by the `user_id`.

html
<div>
    <strong>Last Modified By (Designation):</strong>
    {{ frappe.db.get_value("Employee", { "user_id": doc.modified_by }, "designation") or "N/A" }}
</div>
Next Step
4

Save and Preview the Changes

Save the Print Format. Now, open a document of the corresponding type (e.g., a Sales Invoice) and go to Print > Select your custom format. The user's name and designation should now be visible.

bash
// No code for this step. Preview the print format.

Common Issues & Solutions

Troubleshoot problems you might encounter