1

I've been trying to get my register page to work for a couple days now. I think I'm close , but now I'm receiving the following error message in my terminal when I try to click register from the login page:

File "/home/ubuntu/workspace/pset7/finance/application.py", line 175, in errorhandler

return apology(e.name, e.code)

AttributeError: 'ValueError' object has no attribute 'name'

Here is my code from the lines the error is pointing to (I didn't write them, they were provided with the distribution code):

def errorhandler(e):
"""Handle error"""
return apology(e.name, e.code)

Any help would be greatly appreciated. I will include my register function code from application.py just in case that helps:

 @app.route("/register", methods=["GET", "POST"])
def register():
    """Register user"""
    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # Ensure username was submitted
        if not request.form.get("username"):
            return apology("must provide username", 403)

        # Ensure password was submitted
        elif not request.form.get("password"):
            return apology("must provide password", 403)

        # Ensure password == confirmation
        elif request.form.get("password") != request.form.get("confirmation"):
            return apology("password and confirmation do not match", 403)

        # Ensure username is not already taken
        rows = db.execute("SELECT * FROM users WHERE username = :username",
                          username=request.form.get("username"))
        if len(rows) == 1:
            return apology("username already taken. please choose another", 403)

        # Add new user to the database
        session["user_id"] = db.execute("INSERT INTO users(username, hash) VALUES(:username, :hash)", username=request.form.get("username"), \
        hash=generate_password_hash(request.form.get("password")))

        # Redirect user to home page
        return redirect("/")

Thanks! ~Andy

Edit* Here is the full response leading to the error (is this called a stack trace?)

 * Serving Flask app "application"
 * Running on http://ide50-aherzfeld.cs50.io:8080/ (Press CTRL+C to quit)
 * Restarting with stat
INFO:werkzeug:10.240.0.68 - - [28/Jun/2018 04:53:48] "GET / HTTP/1.1" 302 -
INFO:werkzeug:10.240.1.78 - - [28/Jun/2018 04:53:49] "GET /login HTTP/1.1" 200 -
INFO:werkzeug:10.240.0.68 - - [28/Jun/2018 04:53:49] "GET /static/styles.css HTTP/1.1" 200 -
[2018-06-28 04:54:05,649] ERROR in app: Exception on /register [GET]
Traceback (most recent call last):
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1615, in full_dispatch_request
    return self.finalize_request(rv)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1630, in finalize_request
    response = self.make_response(rv)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1725, in make_response
    raise ValueError('View function did not return a response')
ValueError: View function did not return a response
INFO:werkzeug:10.240.1.78 - - [28/Jun/2018 04:54:05] "GET /register HTTP/1.1" 500 -
ERROR:werkzeug:Error on request:
Traceback (most recent call last):
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/werkzeug/serving.py", line 270, in run_wsgi
    execute(self.server.app)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/werkzeug/serving.py", line 258, in execute
    application_iter = app(environ, start_response)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/opt/pyenv/versions/3.6.0/lib/python3.6/site-packages/flask/app.py", line 1547, in handle_exception
    return self.finalize_request(handler(e), from_error_handler=True)
  File "/home/ubuntu/workspace/pset7/finance/application.py", line 176, in errorhandler
    return apology(e.name, e.code)
AttributeError: 'ValueError' object has no attribute 'name'
3
  • Is there more on the console than just this, like a stack trace? Maybe something where your code is mentioned? The error handler failing at least implies there has been an error it tried to process.
    – Blauelf
    Jun 27 '18 at 10:48
  • Here is the full chain leading to the error (would this be called a stack trace?):
    – aherzfeld
    Jun 28 '18 at 4:55
  • I'm a bit lost because as far as I can see, the problem is not related to the code I've written
    – aherzfeld
    Jun 28 '18 at 4:57
0

I just solved it! I noticed this: ERROR in app: Exception on /register [GET]

So it seemed like the error must be related the the [GET] side of things. I took a look at /login and it had the following else statement at the bottom of its block:

(I changed the login.html to register.html)

# User reached route via GET (as by clicking a link or via redirect)
else:
    return render_template("register.html")

I guess the problem was that I was handling POST requests, but not GET requests. This last else statement ensures that the page will be rendered for someone following the link. Thanks again for the help!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .