Flask - 动态路由不工作 - 数据库

时间:2017-04-27 23:53:01

标签: python html flask jinja2

我正在使用flask和msaccess建立一个网上商店。这是我的着陆页路线的代码。我知道它凌乱。原谅我。着陆页完美运行。我显示了访问者的产品列表。

@picker.route('/')
def featured():
pypyodbc.lowercase = False
conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Users/HP/PycharmProjects/Picker/sv.accdb;'
conn = pypyodbc.connect(conn_string)
cur = conn.cursor().execute('SELECT products.product_name, products.product_description, products.photourl, products.price, products.instock_quantity, vendors.vendor FROM vendors INNER JOIN (categories INNER JOIN products ON categories.category_id = products.category) ON vendors.vendor_id = products.vendor;')
products = [dict(product_name=row[0], product_description=row[1], photourl=row[2], price='${:7,.2f}'.format(row[3]) , instock_quantity=row[4], vendor=row[5]) for row in cur.fetchall()]
pagescur = conn.cursor().execute('SELECT pages.page_title, pages.page_url FROM pages;')
pages = [dict(page = row[0], pageurl=row[1]) for row in pagescur.fetchall()]
user_pagescur = conn.cursor().execute('SELECT user_pages.page_url, user_pages.page_name, user_pages.fontawesome from user_pages;')
user_pages = [dict(page_name = row[1], pageurl=row[0], fontawesome=row[2]) for row in user_pagescur.fetchall()]
categoriescur = conn.cursor().execute('SELECT category FROM categories ORDER BY category')
categories = [dict(category_name = row[0]) for row in categoriescur.fetchall()]
bottom_navcur = conn.cursor().execute('SELECT Nav_Bar_Bottom.page_name, Nav_Bar_Bottom.page_url FROM Nav_Bar_Bottom;')
bottom_nav = [dict(page_name=row[0], page_url=row[1]) for row in bottom_navcur.fetchall()]
conn.close()
return render_template('index.html', products=products, pages=pages, user_pages=user_pages, categories=categories, bottom_nav=bottom_nav)

这是index.html代码

中的相关代码
{% for product in products %}
    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2" style="font-family: 'Josefin Sans', sans-serif; border-left: solid 0.1px rgba(239, 236, 236, 0.78); border-right: solid 0.1px rgba(239, 236, 236, 0.78);">
<a href="{{ url_for('detail', product_name=product['product_name']) }}"><img style="height: 144px;" class="img-responsive img-rounded" src="{{ product.photourl }}"></a>
<p style="text-align: center; color: dodgerblue"><strong>{{ product.price }}</strong></p>
<p style="text-align: center; color: #000000"><a href="{{ url_for('detail', product_name=product['product_name']) }}"><strong><nobr>{{ product.product_name }}</nobr></strong></a></p>
<p style="text-align: center; color: #000000"><strong><button type="button" class="btn btn-default">Add To Cart</button></strong></p>

        {%endfor%}

该部分网站有效。我难以点击其中一个链接,并拉出一个显示product_name,价格,数量,product_description的页面。

这是我的product_detail页面的代码不起作用。我做错了什么?

@picker.route('/detail/<product_name>')
def detail(product_name):
pypyodbc.lowercase = False
conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Users/HP/PycharmProjects/Picker/sv.accdb;'
conn = pypyodbc.connect(conn_string)
cur = conn.cursor().execute('SELECT products.product_name, products.product_description, products.photourl, products.price, products.instock_quantity from products;')
row = cur.fetchone()
print(product_name)
if not row:
    abort(404)
return render_template('testing.html', product_name=row[0], product_description=row[1], photourl=row[2],
                       price='${:7,.2f}'.format(row[3]), instock_quantity=row[4], vendor=row[5])

这是一个简单的模板,我只是为了看看我是否可以得到任何输出。 testing.html

{{ product_name }}

每当我点击index.html中的项目时,我都会收到这样的搜索请求     http://127.0.0.1:5000/detail/Rice%20Krispie%20Treats

后面跟着这个错误。

builtins.IndexError| IndexError: tuple index out of range

我怎样才能使这个工作?任何帮助赞赏。我也不愿意使用sqlalchemy。

Traceback (most recent call last)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\HP\PycharmProjects\Picker\Picker.py", line 56, in detail
price='${:7,.2f}'.format(row[3]), instock_quantity=row[4], vendor=row[5])
File "C:\Users\HP\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\pypyodbc.py", line 1061, in __getitem__
return tuple.__getitem__(self,field)
IndexError: tuple index out of range

1 个答案:

答案 0 :(得分:0)

似乎我在详细功能

中使用此代码解决了我自己的问题
@picker.route('/detail/<product_name>')
def detail(product_name):
pypyodbc.lowercase = False
conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Users/HP/PycharmProjects/Picker/sv.accdb;'
conn = pypyodbc.connect(conn_string)
cur = conn.cursor().execute('SELECT products.product_name, products.product_description, products.photourl, products.price, products.instock_quantity from products where product_name = (?) ;', (product_name,))
for row in cur.fetchall():
    product_description=row[1]
    photourl=row[2]
    price=row[3]
    instock_quantity=row[4]
    print(product_name)

return render_template('products.html', product_name=product_name, price=price, photourl=photourl, instock_quantity=instock_quantity, product_description=product_description)