Reputation: 2924
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
@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.
Upvotes: 5
Views: 6322
Reputation: 10121
For those who are using the render shortcut
and still facing this error, just add {'request': request }
to the context variable
context = { ..., 'request':request}
return render(request, 'templatename.html', context)
Upvotes: 1
Reputation: 198
I also face this error earlier. I was getting following error: Internal Server Error: /cancel-email/
Internal Server Error: /cancel-email/
Traceback (most recent call last):
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 506, in parse
compile_func = self.tags[command]
KeyError: 'static'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/var/www/recruiter-new/recruiter/scheduler.py", line 803, in cancelEmail
return render(request,'scheduler/cancel-email-part.html',{"cancel_email" :EmailDetail})
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/shortcuts.py", line 67, in render
template_name, context, request=request, using=using)
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/loader.py", line 96, in render_to_string
template = get_template(template_name, using=using)
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/loader.py", line 32, in get_template
return engine.get_template(template_name, dirs)
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/backends/django.py", line 40, in get_template
return Template(self.engine.get_template(template_name, dirs), self)
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/engine.py", line 190, in get_template
template, origin = self.find_template(template_name, dirs)
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/engine.py", line 157, in find_template
name, template_dirs=dirs, skip=skip,
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/loaders/base.py", line 46, in get_template
contents, origin, origin.template_name, self.engine,
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 189, in __init__
self.nodelist = self.compile_nodelist()
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 230, in compile_nodelist
return parser.parse()
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 508, in parse
self.invalid_block_tag(token, command, parse_until)
File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 568, in invalid_block_tag
"or load this tag?" % (token.lineno, command)
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 74: 'static'. Did you forget to register or load this tag?
[07/Aug/2018 08:43:26] "POST /cancel-email/ HTTP/1.1" 500 20789
I tried some google solutions but couldn't resolved, then finally again check my code and found a silly mistake on template file.
Just add:
{% load static %}
on top of your template file.
Upvotes: 0
Reputation: 2924
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.
Upvotes: 4
Reputation: 22561
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.
Upvotes: 4