1

I want to take the list created by the following function (list of counters) and print that out on an HTML Django template. I know that in the HTML I'll need

{% for i in listofcounters %}
<p> {{ something here maybe? }} </p>
{% endfor %}

But really the confusion is coming from where the function goes (in views.py? or is it a model?) and then how to get it so the HTML page can read it.

Also, I'm aware that what I might be doing isn't the "best" way to present this information, but this is for me to understand Django better by creating a toy project that I'm interested in. It's not for a "real" site. So, in saying that, I ask that you don't suggest anything too far outside my original question. That being "here's my function, how can I get its output in HTML form?" unless you feel it's impossible to do so.

from mtgsdk import Card

def findcounters():
    listofcounters = []
    cards_from_set = Card.where(set='iko').all()
    for card in cards_from_set:
        if "counter target" in str(card.text).lower():
            listofcounters.append(card.name)
    listofcounters = list(dict.fromkeys(listofcounters))
    return listofcounters

2 Answers 2

1

You can include this in the object you pass to the render inside your view.

from django.shortcuts import render
from mtgsdk import Card


def findcounters():
    listofcounters = []
    cards_from_set = Card.where(set='iko').all()
    for card in cards_from_set:
        if "counter target" in str(card.text).lower():
            listofcounters.append(card.name)
    listofcounters = list(dict.fromkeys(listofcounters))
    return listofcounters


def my_view(request):
    counters = findcounters()
    return render(request, 'file.html', {'counters': counters})

Then import this view into urls and you're off to the races!

Sign up to request clarification or add additional context in comments.

Comments

0

if I am not mistaken all you have to do is pass a dict to the template. I have not tried out your code but from a quick glance that is what I got.

from mtgsdk import Card

def findcounters(request):
    listofcounters = []
    cards_from_set = Card.where(set='iko').all()
    for card in cards_from_set:
        if "counter target" in str(card.text).lower():
            listofcounters.append(card.name)
    listofcounters = list(dict.fromkeys(listofcounters))
    #**********try adding the following code to the code ************
    ctx = { 'listofcounter':listofcounters }

    return render(request, 'yourtemplate.html', ctx}

2 Comments

this function does not serve a request so there is no point of returning render()
@VishalSingh you are right, I missed that, thanks. but this still gets op closer to the solution. or is there a better way?

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.