1

Stuck making my table work please help me figure out this piece.

python code:

@app.route('/books')
def result():
    dict={'Series Title': 'Batman Vol.1: The Count of the Owls', 'Writers': 'Scott Snyder', 'Pencilers': 'Greg Capullo', 'Inkers': 'Jonathan Glapion', 'Volume': 1, 'Issues Contained': 1-7, 'Publisher': 'DC'}
    return render_template('books.html', result=dict)

html/flask code:

<table align="center" id="comic">
        {% for key, value in result %}
        <tr>
            <th> {{ key }} </th>
            <td> {{ value }} </td>
        </tr>
        {% endfor %}
    </table> 

Thanks for the help.

As clarified in the comments below, the particular error I have is “function not defined” for the function “result()”

Patronics
  • 1,351
  • 1
  • 16
  • 27
Zach
  • 49
  • 8
  • What specifically is the problem with this code? Does it not produce a form at all? Produce a form with blank data? Give an error message? Some other problem? It looks fine to me at a quick glance, what is your problem with it in particular? – Patronics Jan 26 '18 at 04:01
  • I get the error function not defined and cant get it defined. – Zach Jan 26 '18 at 15:17
  • I don’t think this question should be closed as a duplicate, it is about using flask in particular, while the question it’s marked as a duplicate of is solely using python, and has a very different solution. @Zach, does it say which function is not defined? If it’s render_template (as I suspect it might be), did you remember to use “from flask import render_template” near the beginning of your code? – Patronics Jan 26 '18 at 15:28
  • yes code is there – Zach Jan 26 '18 at 15:31
  • Does it say which function is not defined? Or specify the line that the error occurs? – Patronics Jan 26 '18 at 15:32
  • result() is not defined as a function – Zach Jan 26 '18 at 15:35
  • Ok, I think I see the problem! You defined result as a function at the beginning “def result():” but later overwrote that when you say “result=dict” then when you later try to call result(), it tries to treat the dictionary that you set to result as a function, which obviously fails! You should be able to fix it by changing the name of either your function at the beginning, or the result variable at the end, so only one of the two is named result, which should fix the error! – Patronics Jan 26 '18 at 15:40
  • Also, can some moderator reopen this question, as it is not a duplicate of the stated answer at all, so I can post a proper answer for it? – Patronics Jan 26 '18 at 15:42
  • did that solution work? – Patronics Jan 26 '18 at 15:52
  • Yes this got my table to work. – Zach Jan 26 '18 at 21:55

2 Answers2

0

for python 2

{% for key, value in result.iteritems() %}

for python 3

{% for key, value in result.items() %}
Ed Marley
  • 19
  • 1
  • 2
  • This answer is not correct, for flask, the way he has (this part of the code anyway) is correct, and the equivalent works fine in my code. – Patronics Jan 26 '18 at 15:23
0

You defined result as a function at the beginning “def result():” but later overwrote that when you say “result=dict” then when you later try to call result(), it tries to treat the dictionary that you set to result as a function, which obviously fails!

You should be able to fix it by changing the name of either your function at the beginning, or the result variable at the end, so only one of the two is named result, which should fix the error!

Zeca
  • 71
  • 1
  • 1
  • 12