Pass and View Dictionary from view to template in django


I am passing the dictionary to the view but it is now showing on the page. i also have print the dictionary on the before passing, and it prints the whole dictionary on the screen perfectly. but when i pass it to the html page, it does not show at all..


def show_log_messages(request): context = RequestContext(request) log_dictionary = {} count = 0 e = log_messages.objects.filter(log_status='Queue').values('sent_to', 'unique_arguments') count = 0 logs = {} for d in e: count +=1 new_dict = {'email': d["sent_to"], 'log_id': d["unique_arguments"]} logs[count] = new_dict for keys in logs: print logs[keys]['log_id'] print logs[keys]['email'] return render_to_response('show_logs.html', logs, context)


{% if logs %} <ul> {% for log in logs: %} {% for keys in log %} <li>{{ log[keys]['email'] }}</li> {% endfor %} </ul> {% else %} <strong>There are no logs present.</strong> {% endif %}

it only show the heading not the list element.


Your code is very unpythonic and undjango. You should pass to template a list instead of dictionary.

Also shortcuts.render is much simpler to use than render_to_response.

def show_log_messages(request): messages = log_messages.objects.filter(log_status='Queue') \ .values('sent_to', 'unique_arguments') logs = [{'email': msg['sent_to'], 'log_id': msg['unique_arguments']} for msg in messages] return render(request, 'show_logs.html', {'logs': logs})


{% if logs %} <ul> {% for log in logs %} <li>{{ log.email }} - {{ log.log_id }}</li> {% endfor %} </ul> {% else %} <strong>There are no logs present.</strong> {% endif %}

BTW, logs list is unnecessary here. You can pass messages queryset directly into template and show {{ log.sent_to }} and {{ log.unique_arguments }} in the <li> tag.


The render_to_response shortcut takes a dictionary. If you want to access logs in the template, it should be in that dictionary:

return render_to_response("show_logs.html", {'logs': logs}, context)

The second problem is that your django template is invalid. It looks like you're trying to write Python in the template. You'd probably find it helpful to read through the <a href="https://docs.djangoproject.com/en/1.7/topics/templates/" rel="nofollow">Django template language docs</a>.

It's not clear to me what you're trying to display, so here's an example of looping through each log, and displaying its id and email. You should be able to adjust this to get the result you want.

{% if logs %} {% for key, value in logs.items %} {{ key }}, {{ key.log_id}}, {{ key.email }} {% endf {% else %} <strong>There are no logs present.</strong> {% endif %}


  • python: remove duplicate values from dict
  • update one dictionary with another by adding values rather then replacing it
  • What is wrong with my ASP.NET MVC SelectList?
  • Combine two list as key value pair using zip if lists have same values
  • firebase unauth with google doesn't allow change of user
  • System call time out?
  • DependencyObject.AssociatedObject is always null
  • Spring Integration Bridge with poller not working as expected for JMS
  • Most efficient way to move table rows from one table to another
  • How to make Twilio api Post request with the help of AFNetworking?
  • Servlet stops working on Tomcat server after some hits or time
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Diff between two dataframes in pandas
  • Breaking out column by groups in Pandas
  • Array with custom indexes in Ionic2
  • Zurb Foundation _global.scss meta styles for js?
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • How do I access an unhandled exception in an MVC Error view?
  • How to modify the colors in the legend of a plot using a fill gradient?
  • How to set ini file attributes during an Inno install
  • Word Open XML Mail Merge
  • Email verification using google app script and google forms
  • Extracting HTML between tags
  • Algorithm for a smudge tool?
  • Email format validation in mvc3 view
  • Read text file and split every line in MSBuild
  • Fetching methods from BroadcastReceiver to update UI
  • Menu Color Fade on Hover with Jquery
  • Check if a string to interpolate provides expected placeholders
  • WinForms: two way TextBox problem
  • Fill an image in a square container while keeping aspect ratio
  • Join two tables and save into third-sql
  • sending/ receiving email in Java
  • Javascript + PHP Encryption with pidCrypt
  • Convert array of 8 bytes to signed long in C++
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Understanding cpu registers
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices