路由到Bottle中的某些页面时,为什么会出现错误“ 404 Not Found”?

时间:2019-10-26 10:31:28

标签: html python-3.x bottle

我正在尝试建立一个简单的购物网站,但是在某些路线上,我一直得到 404 Not Found 。我对Bottle Framework还是很陌生,无法解决问题。任何帮助将不胜感激。

代码如下:

import random
from bottle import Bottle,route,template, static_file, request, redirect, abort, view
import model
import session



app = Bottle()


@app.route('/')
def index(db):

    info = {
        'title': "Our Products"
    }

    all_products = model.product_list(db)
    return template('index', info, products=all_products)


@app.route('/product/<id>', method='GET')
def product(db, id):

    product = model.product_get(db, id)
    return template('product.html', product=product)

@app.route('/views/men', method='GET')
def product(db):

    products = model.product_list(db, 'Men')
    return template('views/men.html', products=products)

@app.route('/', method='POST')
def cart(db):

    id = request.query.id
    quantity = request.forms.get('quantity')
    product = model.product_get(db, id);
    session.add_to_cart(db, id, quantity);

    cart = session.get_cart_contents()
    return template('cart.html', cart=cart)

@app.route('/cart')
def show_cart():
    cart = session.get_cart_contents()
    return template('cart.html', cart=cart)

@app.route('/static/<filename:path>')
def static(filename):
    return static_file(filename=filename, root='static')


if __name__ == '__main__':

    from bottle import run
    from bottle.ext import sqlite, beaker
    from dbschema import DATABASE_NAME
    # install the database plugin
    app.install(sqlite.Plugin(dbfile=DATABASE_NAME))

    # install beaker
    session_opts = {
        'session.type': 'memory',
    }
    beaker_app = beaker.middleware.SessionMiddleware(app, session_opts)
    run(app=beaker_app, debug=True, port=8010)

主页工作正常。它加载了,我可以看到所有产品。当我单击产品时,它将带我到“ http://127.0.0.1:8010/product/1”,一切正常。此时,我单击添加到购物车,这将我带到购物车。但是,当我将路线更改为'/ cart'时,会得到404 Not Found,将其设置为'/'时,它可以正常工作。当我转到“视图/人”时,我得到了404 Not Found。我不明白发生了什么。

0 个答案:

没有答案