How to retrieve the values from the context_process.py file to template?

33 views
Skip to first unread message

ratnadeep ray

unread,
May 26, 2020, 9:26:35 AM5/26/20
to Django users
Hi all, 

I am trying to display the value from the context_process.py file to the template. 

The content of my context_process.py is as follows: 

from fusioncharts.models import QRC_DB
from django.db import connection

def get_result(request):
    cursor  = connection.cursor()
    pattern = "%%"+TotalTime+"%%"
    cursor.execute("select * from fusioncharts_qrc_db")
    rows = cursor.fetchall()
    return {'get_result': rows}

Then in the html template file, I am trying to display the variable get_result as follows: 

{% block content %}
  <h3> Result = </h3>
  <br>
  {{get_result}}

{% endblock %}

My template details in the settings.py is as follows: 

 
TEMPLATES = [
    {
        .......
        'OPTIONS': {
            'context_processors': [
                .........
                'fusioncharts.context_processors.get_result',
            ],
        },
    },
]

My views.py file is as follows: 

def home(request):
    context = RequestContext(request)
    response_context = {}
    return render(request, 'display_data.html',response_context)


But when I am running that, it is not displaying anything in the web page. 

Is there anything missing in my code? What is going wrong here? 





Aboyeji Solomon Adeleke

unread,
May 26, 2020, 10:30:15 AM5/26/20
to Django users
 response_context = {

Aboyeji Solomon Adeleke

unread,
May 26, 2020, 10:30:15 AM5/26/20
to Django users
Your context should be written this way
response_context = get_result(request)


On Tuesday, May 26, 2020 at 2:26:35 PM UTC+1, ratnadeep ray wrote:

ratnadeep ray

unread,
May 26, 2020, 10:46:19 AM5/26/20
to Django users
So for this we have to import the model of context_process ?

Aboyeji Solomon Adeleke

unread,
May 26, 2020, 12:27:13 PM5/26/20
to Django users
Yes, import the get_result from the context_process.py

Daniel Roseman

unread,
May 26, 2020, 1:13:30 PM5/26/20
to Django users
On Tuesday, 26 May 2020 17:27:13 UTC+1, Aboyeji Solomon Adeleke wrote:
Yes, import the get_result from the context_process.py

This is entirely wrong. The point of a context processor is that it is automatically added to the template context. You shouldn't be importing it or calling it specifically in your view.

OP's original code should have worked (although there's a pointless reference to RequestContext that should be deleted).
--
DR.

Durai pandian

unread,
May 26, 2020, 1:39:33 PM5/26/20
to django...@googlegroups.com
I can see typo in adding  'fusioncharts.context_processors.get_result', isn't it context_processor ? 

if there is no typo, please check; if you have any row data by printing it. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a9d314d2-69a4-4b14-b4ed-3f132b28e482%40googlegroups.com.


--
Thanks & Regards,
Durai pandian
Mobile  :
+91-9611051220
Email    : ddp...@gmail.com

ratnadeep ray

unread,
May 26, 2020, 1:46:14 PM5/26/20
to Django users
So what should be the correction in my case?
Reply all
Reply to author
Forward
0 new messages