简体   繁体   English

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

[英]Printing static array result in webpage using django

I'm newbie to django.I want to print static array output in django web page.But when I'm executing the code It shows blank in my web page. 我是django的新手,我想在django网页中打印静态数组输出,但是当我执行代码时,它在我的网页中显示为空白。 So, help me to print my view.py results in web page 因此,请帮助我在网页中打印view.py结果

This my views.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')

This my 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>

first you have to initialise the variables like this : 首先,您必须像这样初始化变量:

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

occurance1 = []
unaccured = []

for i in x:
    .....

You can do like this to pass : 您可以像这样通过:

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

and in your html it will be good if you do like this : 并且在您的html中,如果您这样做会很好:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM