How to order the documents in a DocType's Web View


I have created a Web View of the Brand DocType.

Here is my brand_row.html

<div class='py-8 row'>
	<div class='col-sm-1'>
		<img alt='{{ }}' src='{{ doc.image }}'>
	<div class='col'>
		<a class='font-size-lg' href='{{ doc.route }}'>{{ doc.title or }}</a>
		<p class='text-muted'>By {{ doc.description }}</p>

How can I order these Brands other than by the last date the Document was edited, which seems to be the default ? In other words I want to apply an alphabetical sort order to the Brand Name so that the brands appear in this order :

Thanks so much.


The default sort order is based on the ‘modified’ docfield, and the sort order of the List View has no effect on the sort order of this Web View, neither has the DocType > Brand > Settings > View Settings > Default Sort Field any effect on the order.

So I presume I’d have to look into the template which creates the Web View, but I have no idea of even where to start investigating.

Hi @EugeneP:

You need to define custom get_list_context and get_list methods on your file
It will provide custom “context” for webview.

See this on

Hope this helps.

Thanks so much @avc

I’ve learned a lot from you reply, much appreciated :+1:

However, even changing the order_by=“brand”, does not modify the order. I presume the order_by=“brand” is only for the query from the database, not for the order of display on the Web View ?

I attach my entire, maybe you can spot a Murphy :rofl:

import copy

import frappe
from import WebsiteGenerator

class Brand(WebsiteGenerator):

def get_brand_defaults(item, company):
	item = frappe.get_cached_doc("Item", item)
	if item.brand:
		brand = frappe.get_cached_doc("Brand", item.brand)

		for d in brand.brand_defaults or []:
			if == company:
				row = copy.deepcopy(d.as_dict())
				return row

	return frappe._dict()

def get_brand_list(
	doctype, txt, filters, limit_start, limit_page_length=20, order_by="brand"
	fields = "distinct *"
	return frappe.get_list(
		# or_filters=or_filters,
		# ignore_permissions=ignore_permissions,

def get_list_context(context=None):
	from erpnext.controllers.website_list_for_contact import get_list_context
	list_context = get_list_context(context)
			"show_sidebar": False,
			"show_search": False,
			"no_breadcrumbs": False,
			"title": "Brands",
			"get_list": get_brand_list,
			"row_template": "setup/doctype/brand/templates/brand_row.html",
	return list_context

Hi @EugeneP :

Try with this:

def get_brand_list(doctype, txt, filters, limit_start, limit_page_length=20, order_by=None):
	brand_list = frappe.get_list(doctype="Brand", fields=["name", "brand"], order_by="brand")
	return brand_list

def get_list_context(context=None):
	return {
		"title": "Brands",
		"get_list": get_brand_list,
		"no_breadcrumbs": False,

Hope this helps :wink:

1 Like

Wonderful, thanks so much :muscle:

1 Like