TypeError when index.html is added to exercise (Ex50) [SOLVED]

Hello.

I had to redo everything beacuse I messed up the whole exercise last time. well. Repetition is also a good teacher ( I hope :cold_sweat:.)

The first code in exercise 50 worked fine as last time.
It was when I added the template/index.html to the code I run into a problem I could not see why it occurs. It didnt happened to me last time.

I got this error:

status < 200 or status in (204, 304)): TypeError: '<' not supported between instances of 'str' and 'int'

I uploaded my files ot Github to watch there.
exercise 50 - 51

Please help me to find what I have missed.
I cannot see what causes the problem this time.
And I have no old code to compare anymore :sob:

Ah, that means status is a string, but you’re comparing it to a number. If you do this first:

status_num = int(status)

Then it’ll work.

THello.
Thanks for reply.
My guess was that “status_num = int(status)” was supposed to be in test/test_app.py.
But it didnt help. Except for I had to change “import flask” to “from flask import Flask” my Pytest file works perfekt whitout it (Github updated).

It is when I start the app (python app.py) and refresh the window(browser) I get this error in terminal:

Error on request:
Traceback (most recent call last):
  File "/home/ulf/Documents/python/LPTHW/projects/ex51/flask-venv/lib/python3.6/site-packages/werkzeug/serving.py", line 269, in run_wsgi
    execute(self.server.app)
  File "/home/ulf/Documents/python/LPTHW/projects/ex51/flask-venv/lib/python3.6/site-packages/werkzeug/serving.py", line 260, in execute
    write(data)
  File "/home/ulf/Documents/python/LPTHW/projects/ex51/flask-venv/lib/python3.6/site-packages/werkzeug/serving.py", line 231, in write
    status < 200 or status in (204, 304)):
TypeError: '<' not supported between instances of 'str' and 'int'

Where was the int(status) supposed to be?
I was thinking I put in a " < " somewhere it should’nt be.

I’m not really sure why you’re getting this error then. I’d need to see your code first. Can you link me to it on your github?

Sorry
I forgot to add the link.

Ok, I had to reformat you error message and now I see that this doesn’t seem to be coming from you really but from deep inside Flask. So you’re saying if you change this line:

To use import flask then you get this error in your Pytest, but if you set it to from flask import Flask then you don’t get the error.

I’d say then that’s your fix, and this is mosts likely a bug in flask or how you’re running it. Are you doing flask run to run it?

Hello.

It is not with Pytest I have problem in this exercise. At least not yet.
Its with app.py or index.html there are something I did wrong this time.
Mostly I run it with “python app.py”
I always do this with venv activated. Then I got python -version 3.6.3.

I tried the “flask run” the first time tonight. I got another Error message then:

[File "/home/ulf/Documents/python/LPTHW/projects/ex51/flask-venv/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ulf/Documents/python/LPTHW/projects/ex51/app/app.py", line 9, in index
    return render_templates("index.html", greeting=greeting)
NameError: name 'render_templates' is not defined
File "/home/ulf/Documents/python/LPTHW/projects/ex51/flask-venv/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ulf/Documents/python/LPTHW/projects/ex51/app/app.py", line 9, in index
    return render_templates("index.html", greeting=greeting)
NameError: name 'render_templates' is not defined

I found an error in here. ‘render_templates’ . A letter too much(‘s’).
But correcting this gave me just another error message in the browser:

The connection was reset The connection to the server was reset while the page was loading. The site could be temporarily unavailable or too busy. Try again in a few moments. If you are unable to load any pages, check your computer’s network connection. If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the Web.

I guess I did something wrong this time. It did not happened the first time. But I just cannot see it my self.
perhaps I have to do something else for some days?
This is just a hobby for me. Fortunately I do not have any deadline to follow. Just learn it to make interesting projects.

Ok that’s actually better. Now you need to start trying to debug this rather than assuming something is wrong with your setup. Remember every time you get a NameError it’s only:

  1. You spelled something wrong.
  2. You didn’t define or import something you needed.

In this case, you spelled render_templates but it’s render_template. As you get further errors, look for solutions. Look at every line and also make sure to compare every line that I have to what you have.

Hello @zedshaw.

Thanks for helping me again.
I followed your checklist (and added a little bit more).

  1. It was not any bad spelling anywhere.
  2. I had the required imports for what I was working with.
  3. So I tried a little bit more
  4. And Google a lot.

I got a new error. “TemplateNotFound”
There are some ways to “direct” to templates folder.
I have not tried them yet.
I found another flask project I have that worked fine.
The “app.py” file was in the same folder as templates folder.

projects/
   ex 51/

     app.py (works fine in here)

     app/     (app.py does NOT work in here)
     docs/
     bin/
     tests/ 
     templates/ index.html

This works for me now

I see that you do have a spelling mistake though. You use render_templates but it should be render_template (singular). I believe you might have fixed that, but check it. Next, try doing this:

export PYTHONPATH=.

If you’re on Windows let me know as it’s different. That should force it to use the local directory. Next, you should watch the video. I do this whole setup and if you follow my steps you should be able to figure it out from how I did it.

Hello @zedshaw.

Yes. The spelling is corrected.
So is also the directory structure to be like the one you described in ex50.
I think the mistakes I did was that the app.py and templates folder was not in the same directory. When I found out it worked again.
My “Tree” looks like this now:

projects/
        gothonweb/
                app.py
                templates/
        docs/
        bin/
        venv/
        tests/
              test_app.py

I just continued with ex 50 into ex51.
Now I am learning more about Flask and Pytest to do the test part and study drills for this lesson(ex51).

1 Like

Ah, yes that’d do it. Glad you figured it out.