@TurkerTunali
@brian_pond
Great to hear that I’m not alone and that you guys strive to learn and teach each other!
Well, here’s the good news: I finally figured out how to do it. I have realized that I was overthinking everything that i was doing, questioning the control flow of the framework and every little component that it has. I’ve been upholding a “do-first-ask-questions-later” sort of policy.
It is, indeed, a long, sometimes tedious process of trial and error; just as some like to compare coding to writing or construction work, I like to compare it to building a puzzle.
The solution was very simple: in my case, in order to display a table of customers and their respective orders, I made an HTML file in the www folder along with a corresponding Python file. Then, in the Python file, I just imported the frappe module and used the get_context(context)
function to assign an instance variable of context
to a list (or dictionary, I think) containing DocType objects. By using this context function, you initialize the variable that would serve as the pool of the data you want to display.
(Note: for some reason, the frappe.get_all([DocType], fields=[fields], etc.)
function, which returns a list (or dictionary—l can’t remember) of all objects that are of a certain DocType, does not work at the moment, and I have made a post about this newfound, pesky problem; so, for the time being, I decided to take a crude approach by making a list of objects that are of the “Order” DocType by calling the frappe.get_doc([DocType], [name])
function ad nauseum, which is intended to retrieve data from a single object.
import frappe
def get_context(context):
context.orders = [frappe.get_doc("Order", "2995f8477d"),
frappe.get_doc("Order", "170fe55ed2"),
frappe.get_doc("Order", "98eb82155c"),
frappe.get_doc("Order", "39e10adb32"),
frappe.get_doc("Order", "a0ecfce3de")]
''' Currently defunct '''
def get_orders():
return frappe.get_all("Order",
fields=["first_name", "last_name", "address", "order"],
order_by="last_name desc")
Then, in the HTML file, I made a fancy table that lists data from the documents via a simple loop:
<h2> Orders </h2>
{% if orders %}
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Items</th>
<th>Total</th>
</tr>
{% for order in orders %}
<tr>
<td>{{ order.first_name }} {{ order.last_name }}</td>
<td>{{ order.address }}</td>
<td>
<ul style="list-style-type:disc">
{% for i in order.order %}
<li>{{ frappe.get_doc('Product', i.item).name1 }}</li>
{% endfor %}
</ul>
</td>
<td> </td>
</tr>
{% endfor %}
</table>
{% else %}
<p style="font-size:36px; ">No orders!</p>
{% endif %}
And I got this!
Screenshot_20170817_153849|690x388