ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

Webpage get_context / data source

I have main.html & main.py . I have a doctype Main in which I store data to be displayed in main.html.

Now I want to pull data from doctype Event so as to display data in main.html

I been trying to add different codes in def get_context(context) in main.py but havent got anything yet, any suggestions?

Check this page, it has a .py and needs data from OAuth Client and OAuth Bearer Token

Jinja2 / HTML:


py:

For screenshots refer

2 Likes

@revant_one I tried something like this

def get_context(context):
context.doc = frappe.get_doc("Main", "Main")
event = frappe.get_all('Event', filters={'event_type': 'Public'}, fields=["subject", "start_on", "ends_on", "location", "description"])

event_list = []

for item in event:
	app = {
		"subject": item.subject,
		"start_on": item.start_on,
		"ends_on" : item.ends_on,
		"location": item.location,
		"description": item.description
	}
	event_list.append(app)
context.event = event_list

Html

{%- for eve in event -%}
			<div class="col-md-6 col-sm-12">
				<div class="stm-event stm-event_view_grid">
				<div class="stm-event__body">
					<div class="stm-event__left">
														<div class="stm-event__date">
								<div class="stm-event__date-day">{{eve.start_on}}</div>
								<div class="stm-event__date-month">April</div>
							</div>
												</div>
					<div class="stm-event__content">
						<div class="stm-event__meta">
							<ul>
								<li>
									<span class="stm-event__time"><i class="fa fa-clock-o"></i>	{{eve.start_on}}	</span>
									</li>
							<li><span class="stm-event__venue"><i class="fa fa-map-marker"></i>{{eve.location}} </span></li>
							</ul>
						</div>
						<h5 class="stm-event__title"><a href="/main">{{eve.subject}}</a></h5>
						<div class="stm-event__summary"><p>{{eve.description}}</p>
{%- endfor %}

But my result is blank… what might be wrong?

Why using get_doc(“Main”, “Main”)? is Main dt single? no need to put it in context if it’s not used.

try printing context.event or event_list on bench log.

or try if event|length > 0 print events else print “No Events”

def get_context(context):
	context_doc = frappe.get_doc("Main", "Main")
	context_event = frappe.get_all("Event", filters={
		"event_type": "Public"
	}, fields=["*"])
    	return {
		"doc": context_doc,
		"event": context_event
	      }

Thanks @revant_one . This worked. Yes Main is a dt and holds data i want apart from event

@revant_one ok one question . I have main.html, main.py both in www folder of frappe. Now i been able to add events which is the standard event doctype in frappe. WHen i add new events it doesnt get dynamically added to the main.html . I need to restart server for changes to be shown?

In production setup it should work without restart

have you tried no_cache = 1 ?

2 Likes

Can anyone help me please?

I’ve always get the following error:

    Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 39, in render
    data = render_page_by_language(path)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 133, in render_page_by_language
    return render_page(path)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 149, in render_page
    return build(path)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 156, in build
    return build_page(path)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/website/render.py", line 169, in build_page
    context = get_context(path)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/website/context.py", line 28, in get_context
    context = build_context(context)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/website/context.py", line 100, in build_context
    update_controller_context(context, context.controller)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/website/context.py", line 37, in update_controller_context
    module = frappe.get_module(controller)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 704, in get_module
    return importlib.import_module(modulename)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named www.index

my “index.py” looks like that:

from __future__ import unicode_literals
import frappe


def get_context(context):
    context.sliders = frappe.db.sql("select image from `tabSlider`")
    context.marks = frappe.db.sql("select trade_mark_name,image,name from `tabTrade Mark`")
    context.news = frappe.db.sql("select title,image,subject,name from `tabNews`")

@joelios i think you www folder is not an valid python module.

You should add an empty __init__.py file inside this folder to turn it an valid python module.

3 Likes

@max_morais_dmm thank you very much :slight_smile: now it works nice :+1:

Things are not working with me but. Toiling for 2 hrs for this.

In www folder made :

  1. init.py file
  2. student-profile.py
  3. sudent-profile.html

the contents of py and html are as follows:

student-profile.py:

from __future__ import unicode_literals

import frappe
from frappe import _

no_cache = 1
no_sitemap = 1

def get_context(context) :
    context.no_cache = True
    context.profile = "JJ"

and student-profile.html:



<h2>Student profile</h2>

{{profile}}



This is how it looks when i open the parth localthost:myport/student-proflle

If anyone facing similar issues can help ? @max_morais_dmm @joelios @revant_one @vivek

1 Like

Try dropping hyphen from module name student-profile.py.
make it student_profile.py

2 Likes

add <!-- jinja --> in html

<!-- jinja -->
<h2>Student profile</h2>

<div>{{profile}}</div>

Really helpful comments. Thanks