Frappe and ERPNext homepage

Customizing frappe/erpnext homepage

Goal

This post explains how we can set homepage in a erpnext/frappe installation.

Default frappe installation doesn’t provide a homepage. You can create and set a homepage in several ways as described below:

Setup

This post assumes that you have a Frappe project initialized and you are able to add a site to your frappe installation.

Let’s add a site

$ bench new-site foo.bar

Let’s create an app and install it in the site foo.bar.

$ bench new-app meeting
$ bench --site foo.bar install-app meeting

Default behaviour

If you are logged in as an administrator and access the homepage i.e foo.bar:8002, you would be redirected to the desk environment.

If you aren’t logged in as administrator, i.e for a Guest visitor, accessing homepage redirects to login page.

We want to change this behaviour and instead of redirecting to login page we want to show some content.

There are several alternatives provided by frappe to add a homepage. They are:

  • Using Website Settings
  • Using home_page attribute of hooks.py
  • Using get_website_user_home_page attribute of hooks.py
  • Using role_home_page attribute of hooks.py

Using Website Settings

Homepage can be set using Home Page field of Website Settings.

You can set it to any valid route of your project. There are some routes provided by default in any Frappe installation. eg: about and contact.

Access path /about and you should be able to see a page.

You can set about as homepage of your application in the following way:

Clear cache and reload the homepage, and your about page content should show on homepage.

You can create a Web Page from the desk and set it as the homepage instead. You can learn about creating Web Pages from our other post.

Assuming you have created a Webpage with route what-we-do which looks like:

You can set what-we-do as homepage of your application in the following way:

Clear cache and reload the homepage, and your what-we-do page should show on homepage.

You can set a custom webview added in www/ as your homepage too.

Assume we have a web view at meeting/www/custom-homepage.html with associated controller at meeting/www/custom_homepage.py. Read our last post to understand web view in detail.

Content of meeting/www/custom-homepage.html could be:

{{body}}

Content of meeting/www/custom_homepage.py could be:

def get_context(context):
    context['body'] = 'This is a custom homepage'

Set Home Page of Website Settings to custom-homepage.

Clear cache and reload the homepage. It should show content of web view custom-homepage.

Using home_page attribute

You can set homepage using home_page variable of hooks. This takes precedence over Website Settings homepage.

Edit meeting/hooks.py and add module level attribute home_page.

home_page = "about"

Clear cache and load the homepage. It should show content of about page on homepage now.

As with Website Settings Home Page, you can point home_page hook to a www/ webview too.

Edit home_page attribute of meeting/hooks.py to point to this web view route.

home_page = "custom-homepage"

Clear cache and load the homepage. It should show content of web view custom-homepage.

Thank you for reading the Agiliq blog. This article was writen by Akshar on Aug 8, 2018 in pythonfrappeerpnext .

You can subscribe ⚛ to our blog.

We love building amazing apps for web and mobile for our clients. If you are looking for developmenrt help, contact us today ✉.

Would you like to download 10+ free Django and Python books? Get them here