Python - BaseHTTPServer, проблема с POST и GET

Сейчас я делаю очень простое приложение с двумя веб-страницами по URL-адресам: localhost: 8080 / restaurant / и localhost: 8080 / restaurant /new. У меня есть база данных sqlite, которой я манипулирую с SQLAlchemy в моем коде Python.

На моей первой странице localhost: 8080 / Restaurant /, это просто список ресторанов, доступных в моей базе данных. Моя вторая страница localhost: 8080 / restaurant /new, там у меня есть форма для нового ресторана, которая отображается на localhost: 8080 / restaurant. Однако всякий раз, когда я вписываю имя нового ресторана в форму на localhost: 8080 / restaurant /new, он не перенаправляет меня обратно на localhost: 8080 / restaurant /, чтобы показать мне новый ресторан, вместо этого он просто остается на той же URL-ссылке. localhost: 8080 / restaurant / new с сообщением "Данные не получены" . Ниже мой код:

import cgi
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

#import libraries and modules
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Base, Restaurant, MenuItem

#create and connect to database
engine = create_engine('sqlite:///restaurantmenu.db')
Base.metadata.bind=engine
DBSession = sessionmaker(bind=engine)
session = DBSession()


class webServerHandler(BaseHTTPRequestHandler):
    """ class defined in the main method"""

    def do_GET(self):
        try:
            #look for url then ends with '/hello'
            if self.path.endswith("/restaurants"):
                self.send_response(200)

                #indicate reply in form of html to the client
                self.send_header('Content-type', 'text/html')

                #indicates end of https headers in the response
                self.end_headers()

                #obtain all restaurant names from databse
                restaurants = session.query(Restaurant).all()

                output = ""
                output += "<html><body><a href='/restaurants/new'>Add A New Restaurant</a>"
                output += "</br></br>"

                for restaurant in restaurants:
                    output += restaurant.name
                    output += """<div>
                            <a href='#'>Edit</a>
                            <a href='#'>Delete</a>
                            </div>"""
                    output += "</br></br>"
                output += "</body></html>"

                self.wfile.write(output)
                print output
                return


            if self.path.endswith("/restaurants/new"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()

                output = ""
                output += "<html><body>"
                output += "<h1>Add New Restaurant</h1>"

                output += "<form method='POST' enctype='multipart/form-data action='/restaurants/new'>"
                output += "<input name='newRestaurant' type='text' placeholder='New Restaurant Name'>"
                output += "<input name='Create' type='submit' label='Create'>"
                output += "</form></body></html>"

                self.wfile.write(output)
                return

        except IOError:

            self.send_error(404, "File %s not found" % self.path)



    def do_POST(self):

        try:
            if self.path.endswith("/restaurants/new"):

                ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))

                #check of content-type is form
                if ctype == 'mulitpart/form-data':

                    #collect all fields from form, fields is a dictionary
                    fields = cgi.parse_multipart(self.rfile, pdict)

                    #extract the name of the restaurant from the form
                    messagecontent = fields.get('newRestaurant')

                    #create the new object
                    newRestaurantName = Restaurant(name = messagecontent[0])
                    session.add(newRestaurantName)
                    session.commit()

                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location','/restaurants')
                    self.end_headers()

        except:
            pass




def main():
    """An instance of HTTPServer is created in the main method 
    HTTPServer is built off of a TCP server indicating the 
    transmission protocol
    """
    try:
        port = 8080

        #server address is tuple & contains host and port number
        #host is an empty string in this case
        server = HTTPServer(('', port), webServerHandler)


        print "Web server running on port %s"  % port

        #keep server continually listening until interrupt occurs
        server.serve_forever()


    except KeyboardInterrupt:
        print "^C entered, stopping web server...."

        #shut down server
        server.socket.close()


#run main method
if __name__ == '__main__':
    main()

для справки вот мой файл database_setup, где я создаю базу данных:

import sys

#importing classes from sqlalchemy module
from sqlalchemy import Column, ForeignKey, Integer, String

#delcaritive_base , used in the configuration
# and class code, used when writing mapper
from sqlalchemy.ext.declarative import declarative_base

#relationship in order to create foreign key relationship
#used when writing the mapper
from sqlalchemy.orm import relationship

#create_engine to used in the configuration code at the
#end of the file
from sqlalchemy import create_engine


#this object will help set up when writing the class code
Base = declarative_base()



class Restaurant(Base):
    """
    class Restaurant corresponds to restaurant table
    in the database to be created.

    table representation for restaurant which
    is in the database 
    """
    __tablename__ = 'restaurant'


    #column definitions for the restaurant table
    id = Column(Integer, primary_key=True)
    name = Column(String(250), nullable=False)



class MenuItem(Base):
    """
    class MenuItem corresponds to restaurant table


    table representation for menu_item which
    is in the database      
    """
    __tablename__ = 'menu_item'

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



#create an instance of create_engine class
#and point to the database to be used
engine = create_engine(
    'sqlite:///restaurantmenu.db')


#that will soon be added into the database. makes
#the engine
Base.metadata.create_all(engine)

Я не могу понять, почему я не могу добавить новые рестораны

2 ответа

Решение

Я знаю, это было давно, но я разобрался с твоей проблемой. Во-первых, enctype='multipart/form-data' в вашей функции do_GET под if self.path.endswith("/restaurants/new"): часть отсутствует окончательная одинарная кавычка. Во-вторых, вы ошиблись 'multipart' в if ctype == 'multipart/form-data':, Надеюсь, что это может помочь вам или другим.

Как сказал Штивен, проблема была в типе шифрования в форме.

Поскольку цитата была пропущена, "Content-type" изменился на "application/x-www-form-urlencoded", поэтому в этом случае вы должны проанализировать его по-разному, так как это строка.

Для управления обоими типами enctype вы можете изменить ваш do_POST следующим образом

def do_POST(self):

    try:

        if self.path.endswith("/restaurants/new"):

            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))

            print ctype

            #check of content-type is form
            if (ctype == 'multipart/form-data') or (ctype == 'application/x-www-form-urlencoded'):

                #collect all fields from form, fields is a dictionary
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                else:
                    content_length = self.headers.getheaders('Content-length')
                    length = int(content_length[0])
                    body = self.rfile.read(length)
                    fields = urlparse.parse_qs(body)

                #extract the name of the restaurant from the form
                messagecontent = fields.get('newRestaurant')

                #create the new object
                newRestaurantName = Restaurant(name = messagecontent[0])
                session.add(newRestaurantName)
                session.commit()

                self.send_response(301)
                self.send_header('Location','/restaurants')
                self.end_headers()
                return

Надеюсь, что эта дополнительная информация полезна для вас!

Другие вопросы по тегам