簡體   English   中英

使用Django在網頁中打印靜態數組結果

[英]Printing static array result in webpage using 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>

首先,您必須像這樣初始化變量:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM