0

I am new with html and I'm trying to display two lists into a table on the html page.

I can display the lists but can't get them to display as a nice table.

My code is:

from tabulate import tabulate 

def some_function(a, b, c)
    cols = ['Col 1', 'Col 2', 'Col3']
    vals = [a, b, c]
    table = [cols, vals]
    table = tabulate(table, tablefmt='html')
    return table
some_function(1.01, 1.03, 1.05)

This returns the following html code:

<table>
<tbody>
<tr><td>Col 1</td><td>Col 2</td><td>Col 3</td></tr>
<tr><td>1.01     </td><td>1.03      </td><td>1.05     </td></tr>
</tbody>
</table>

I am using Django so I can successfully return the html script to the website with my View but it doesn't read it as code, it appears as a string:

View:

 def post(self, request):
        form_c = CalculatorForm(request.POST, prefix='form_c')
        try:
            if form_c.is_valid():
                post = form_c.cleaned_data
                numbers = some_function(1.01, 1.03, 1.05)

        except:
            pass

        args = {
            'form_c': form_c, 'form_cols': numbers,
        }
        return render(request, self.template, args)

my html:

    <div class="container">
        {{ form_cols }} # <-- where I want my table
    </div>
3
  • This code looks fine. What do you mean by "nice table"? Commented Aug 2, 2020 at 3:45
  • what's the templating engine? Commented Aug 2, 2020 at 3:50
  • @anandogc I want that html code snippet to work in the html argument ‘form_cols’ but it returns the snippet as a string not acting like code Commented Aug 2, 2020 at 3:53

1 Answer 1

1

In the html write (assuming default template engine):

    <div class="container">
        {{ form_cols|safe }}
    </div>
Sign up to request clarification or add additional context in comments.

Comments

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.