0

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. So, help me to print my view.py results in web page

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

{% 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
  • You haven't sent the occurence1 to the front-end Commented Apr 11, 2018 at 6:56

1 Answer 1

1

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 :

{% if occurance1 %}
{% for occurance in occurance1 %}
Sign up to request clarification or add additional context in comments.

5 Comments

But it shows only this --> 67 Occur(s) 2 times ,23 not in list. I want all results
so you do like occurance1.append(i) instead of this occurance1 = i,"Occur(s)",len(c),"times"
It shows this errror .-->'tuple' object has no attribute 'append'
have you done occurance1 = [ ] or occurance1= ( ) ?
Ok.I got all.Thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.