I'm trying to update a restaurant name in the database and getting an error.

The website prints out all restaurant names, then user selected "edit" button under the restaurant that they want to change the name of. Before going to another page, I query the database with the name of the restaurant, retrieve the id, then I build insert the PK into the URL.

I then break down the URL and retrieve back the PK in the POST method and try and update the database with restaurant name with the user chosen name.

Below is the code and current error:

webserver.py

```
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Restaurant, Base, MenuItem
import cgi

class webServerHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            if self.path.endswith("/hello"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += "<html><body>Hello!"
                output += '''<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
                output +="</body></html>"
                self.wfile.write(output)
                print output
                return

            if self.path.endswith("/hola"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += "<html><body> &#161 Hola ! <a href = '/hello'> Back to Hello </a></body></html>"
                output += '''<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
                output +="</body></html>"
                self.wfile.write(output)
                print message
                return

            if self.path.endswith("/restaurants"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                engine = create_engine('sqlite:///restaurantmenu.db')
                Base.metadata.bind = engine
                DBSession = sessionmaker(bind = engine)
                session = DBSession()
                items = session.query(Restaurant).all()
                output = "<html><body> <br> Printing all Restaurants ! </br>"
                for item in items:
                    output += "<br>" + item.name + "</br>"
                    output += "<br> <a href = '/" + str(item.id) + "/rename'> Edit </a></br>"
                    output += "<br> <a href = '/" + str(item.id) + "/delete'> Delete </a></br>"
                output += "<br> <a href = '/new'> Create new restaurant </a></br>"
                output += "</body></html>"
                self.wfile.write(output)
                print output
                return

            if self.path.endswith("/new"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += "<html><body>Make a New Restaurant"
                output += '''<form method='POST' enctype='multipart/form-data' action='/new'><h2>Name of new restaurant ?</h2><input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
                output +="</body></html>"
                self.wfile.write(output)
                print output
                return

            if self.path.endswith("/rename"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                x = self.path
                x = x[:-7]
                x = x[1:]
                engine = create_engine('sqlite:///restaurantmenu.db')
                Base.metadata.bind = engine
                DBSession = sessionmaker(bind = engine)
                session = DBSession()
                items = session.query(Restaurant).filter_by(id = x)
                output = ""
                for item in items:
                    output += "<html><body><h2>" + item.name + "</h2>"
                output += '''<form method='POST' enctype='multipart/form-data' action='/rename'><h2>New name of restaurant ?</h2><input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
                output +="</body></html>"
                self.wfile.write(output)
                print output
                print x
                return

        except IOError:
            self.send_error(404, 'File Not Found: %s' % self.path)

    def do_POST(self):
        if self.path.endswith("/new"):
            self.send_response(301)
            self.send_header('Content-type','text/html')
            self.end_headers()
            ctype, pdict = cgi.parse_header(
            self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('message')
            engine = create_engine('sqlite:///restaurantmenu.db')
            Base.metadata.bind = engine
            DBSession = sessionmaker(bind = engine)
            session = DBSession()
            myrest = Restaurant(name = messagecontent[0])
            session.add(myrest)
            session.commit()
            output = "Restaurant added"
            output +="<br><a href = '/restaurants'> Back to restaurants </a></br>"
            output +="</body></html>"
            self.wfile.write(output)
            print output

        if self.path.endswith("/rename"):
            self.send_response(301)
            self.send_header('Content-type','text/html')
            self.end_headers()
            y = self.path
            y = y[:-7]
            y = y[1:]
            print y
            ctype, pdict = cgi.parse_header(
            self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('message')
            engine = create_engine('sqlite:///restaurantmenu.db')
            Base.metadata.bind = engine
            DBSession = sessionmaker(bind = engine)
            session = DBSession()
            findrest = session.query(Restaurant).filter_by(id = y).one()
            findrest.name = messagecontent[0]
            session.add(findrest)
            session.commit()
            output = "Restaurant " + messagecontent[0] + " added"
            output +="<br><a href = '/restaurants'> Back to restaurants </a></br>"
            output +="</body></html>"
            self.wfile.write(output)
            print output

def main():
    try:
        port = 8080
        server = HTTPServer(('', port), webServerHandler)
        print "Web Server running on port %s" % port
        server.serve_forever()
    except KeyboardInterrupt:
        print " ^C entered, stopping web server...."
        server.socket.close()

if __name__ == '__main__':
    main()

```

database\_setup.py

```
import os
import sys
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base = declarative_base()

class Restaurant(Base):
    __tablename__ = 'restaurant'

    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)

class MenuItem(Base):
    __tablename__ = 'menu_item'

    name = Column(String(80), nullable=False)
    id = Column(Integer, primary_key=True)
    description = Column(String(250))
    price = Column(String(8))
    course = Column(String(250))
    restaurant_id = Column(Integer, ForeignKey('restaurant.id'))
    restaurant = relationship(Restaurant)

engine = create_engine('sqlite:///restaurantmenu.db')

Base.metadata.create_all(engine)

```

error from webserver

```
1
10.0.2.2 - - [30/Mar/2016 10:10:46] "POST /rename HTTP/1.1" 301 -

----------------------------------------
Exception happened during processing of request from ('10.0.2.2', 59743)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request
    method()
  File "webserver.py", line 132, in do_POST
    findrest = session.query(Restaurant).filter_by(id = int(y)).one()
ValueError: invalid literal for int() with base 10: ''
----------------------------------------

```

You can see Y gets printed as 1 in the above