Pass and View Dictionary from view to template in django

强颜欢笑 提交于 2019-12-25 04:53:24

问题


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..

view.py

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)

show_logs.html

{% 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.


回答1:


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})

Template:

{% 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.




回答2:


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 Django template language docs.

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 %}


来源:https://stackoverflow.com/questions/27861078/pass-and-view-dictionary-from-view-to-template-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!