使用django打印静态数组结果在网页中

时间:2018-04-11 06:55:06

标签: python django

我是django的新手。我想在django网页上打印静态数组输出。但是当我执行代码时它在我的网页中显示为空白。 所以,帮我在网页上打印我的view.py结果

这是我的views.py

from django.shortcuts import render

from django.http import HttpResponse

from django.shortcuts import render

def index(request):

    return HttpResponse("<h>Welcome</h>")

def Compare(request):

    x=[98,8998,9,67,23]
    y=[67,37,9,330,123,67,56,123]
    x1=[2103,23,203,12,23,12]
    y1=[213,23,23,12,22,12,21,21]

    for i in x:
        if i in y:
            c=[ii for ii,val in enumerate(y) if val==i] #print c
            occurance1 = i,"Occur(s)",len(c),"times" #Total number of matches
            for j in c:
                if x1[j]==y1[j]:
                    match2=i,"Secondary level match" 
                else:
                    match1= i,"Primary level match"
                    #return match1
        else:
            unoccured= i,"not in list" ##No matches
            #return unoccured
    return render(request,'web/base.html')

这是我的HTML

<html>

{% load staticfiles %}

<title>My site </title>

{% block content %}

<body>

{% for occurance in occurance1 %}

  <h> {{ occurance }}</h>

{% endfor %}

<ul>

{% for a in unaccured %}

<p>{{a}}</p>

{% endfor %}

</ul>

</body>

{% endblock %} </html>

1 个答案:

答案 0 :(得分:1)

首先你需要初始化这样的变量:

y1=[213,23,23,12,22,12,21,21]

occurance1 = []
unaccured = []

for i in x:
    .....

您可以这样做以通过:

context = {'occurance1': occurance1,'unaccured': unaccured}
return render(request,'web/base.html', context)

并且在你的html中,如果你这样做会很好:

{% if occurance1 %}
{% for occurance in occurance1 %}