在Django中将视图从字典传递并查看到模板 [英] Pass and View Dictionary from view to template in django
问题描述
我正在将字典传递给视图,但是现在它显示在页面上. 在通过之前,我也已经将字典打印了,并且它在屏幕上完美地打印了整个字典.但是当我将其传递给html页面时,它根本不显示.
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 %}
它仅显示标题,而不显示列表元素.
您的代码非常不符合Python和undjango.您应该将列表而不是字典传递给模板.
shortcuts.render
比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
列表在这里是不必要的.您可以将messages
查询集直接传递到模板中,并在<li>
标记中显示{{ log.sent_to }}
和{{ log.unique_arguments }}
.
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.
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.
这篇关于在Django中将视图从字典传递并查看到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!