Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keyError in Django. During template rendering

Sorry if its a noob code or question. I am doing pagination using django-pagination and I am doing it like this.But this gives me keyError at on my page furthermore it mentions that its an error during template rendering. What I am doing wrong here.I have successfully installed pagination, modified the settings.py file. But i dont know what i need to do here. Any help would be highly appreciated.

 <table class="active_table"  summary="active_user">
    <thead>
     <tr><th>Name</th><th>Mobile Number</th><th>Count</th></tr>
    </thead>
    <tbody id="table_content">
     {% load pagination_tags %}
       {% block content %}
         {% autopaginate response_data 5 %}
           {% for b in response_data %}
              <tr class="table_rows"><td>{{ b.name }}</td><td>{{ b.mobile_number }}</td><td>{{ b.count }}</td></tr>
           {% endfor %}
         {% paginate %}
        {% endblock %}
     </tbody>
  </table>

The detailed traceback is pasted here http://dpaste.com/919526/

The viewcode is following

views.py

@csrf_exempt

def active_user_table(request, b): if request.method != "GET": raise Http404

if (b=='4'):
         cursor = connection.cursor()
         cursor.execute("SELECT core_user.id, name,mobile_number,COUNT(*) as count, created FROM core_user,core_useractivity WHERE core_user.id = core_useractivity.user_id GROUP BY user_id ORDER BY count DESC")
         response_data = dictfetchall(cursor)
         return render_to_response("siteadmin/active_user_table.tmpl",{'response_data':response_data})
elif (b=='3'):
         cursor = connection.cursor()
         cursor.execute("SELECT core_user.id, name, mobile_number, COUNT(*) as count, created FROM core_user, core_useractivity WHERE core_user.id = core_useractivity.user_id AND MONTH(CAST(created as date)) = MONTH(NOW()) AND YEAR(cast(created as date)) = YEAR(NOW()) GROUP BY user_id ORDER BY count DESC")
         response_data = dictfetchall(cursor)
         return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data})
elif (b=='2'):
         cursor = connection.cursor()
         cursor.execute("SELECT core_user.id, name, mobile_number, COUNT(*) as count, created FROM core_user, core_useractivity WHERE core_user.id = core_useractivity.user_id AND DATEDIFF(NOW(), created) <= 7 GROUP BY user_id ORDER BY count DESC")
         response_data = dictfetchall(cursor)
         return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data})
elif (b=='1'):
         cursor = connection.cursor()
         cursor.execute("SELECT core_user.id, name, mobile_number, COUNT(*) as count, created FROM core_user, core_useractivity WHERE core_user.id = core_useractivity.user_id AND DATE(created) = DATE(NOW())")
         response_data = dictfetchall(cursor)
         return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data})
else:
         raise Http404

Sorry I am not using django ORM as of now but I will do so in future.

like image 214
John Doe Avatar asked Feb 11 '13 09:02

John Doe


2 Answers

You must add context_instance in render_to_response call:

return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data}, context_instance=RequestContext(request))

or you can use TEMPLATE_CONTEXT_PROCESSORS tuple in your settings.py. Add this string "django.core.context_processors.request" to context processors and every RequestContext will contain a variable request.

like image 124
ndpu Avatar answered Nov 03 '22 01:11

ndpu


I solved it myself but thanks to ndpu for helping me atleast I got confident there were no other issue but some settings issue. In this question I have problems with setting up django-pagination. Alasdair had mentioned adding "django.contrib.auth.context_processors.auth", to TEMPLATE_CONTEXT_PROCESSORS . On just adding it I am getting the correct expected values.

like image 36
John Doe Avatar answered Nov 03 '22 01:11

John Doe