Я делаю проект веб-сайта flask и пытаюсь создать отдельную страницу элемента для каждого из моих элементов. Я использую sqlite для базы данных.
Ниже html код
{% extends 'base.html' %}
{% block main %}
<!-- display products for the different categories stock -->
<section>
<div class="text-dark text-center">
<div>
<p class="h3 text-dark mb-4">New Arrival</p>
</div>
</div>
<div class="card-deck">
<!-- each product is a bootstrap card -->
{% for product in products%}
<div class="card">
<img class="card-img-top" src="{{url_for('static', filename='img/'+product.image)}}" alt="Card image">
<div class="card-body">
<h4 class="card-title">{{product.name}}</h4>
<p class="card-text">{{product.description}}</p>
</div>
<div class="card-footer">
<form action="{{ url_for('main.order') }}">
<input type="hidden" id="product_id" name="product_id" value="{{ product.id }}">
<input class="btn btn-primary" type="submit" value="Add to Cart">
</form>
<div class="card-footer"><a href= "{{ url_for('main.itemdetails', productid=product.id) }}" class="btn btn-primary">Item Details</a></div>
</div>
</div>
{% endfor %}
</div>
</section>
{% endblock %}
с использованием маршрута
@bp.route('/<int:productid>/')
def itemdetails():
products= Product.query.one('productid')
return render_template('itemdetails.html', products = products)
my itemdetails. html page:
{% extends 'base.html' %}
{% block main %}
>
<div class="text-dark text-center">
<div>
<p class="h3 text-dark mb-4" id="cities_anchor">Available Products</p>
</div>
</div>
<div class="card-deck">
{% for product in products%}
<div class="card">
<img class="card-img-top" src="{{url_for('static', filename='img/'+product.image)}}" alt="Card image">
<div class="card-body">
<h4 class="card-title">{{product.name}}</h4>
<p class="card-text">{{product.description}}</p>
</div>
<div class="card-footer">
<form action="{{ url_for('main.order') }}">
<input type="hidden" id="product_id" name="product_id" value="{{ product.id }}">
<input class="btn btn-primary" type="submit" value="Add to Cart">
</form>
</div>
</div>
{% endfor %}
</div>
</section>
{% endblock %}
получение сообщения об ошибке TypeError: itemdetails () получил неожиданный аргумент ключевого слова 'productid'
Проблема в том, что мне нужны интерактивные сведения об элементе, которые показывают только информацию о продукте, по которому был нажат пользователь на. Как я могу это сделать?
Моя база данных
from . import db
class Stock(db.Model):
__tablename__='stocks'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
description = db.Column(db.String(500), nullable=False)
image = db.Column(db.String(60), nullable=False, default = 'barbel.jpg')
products = db.relationship('Product', backref='Stock', cascade="all, delete-orphan")
def __repr__(self):
str = "Id: {}, Name: {}, Description: {}, Image: {}\n"
str =str.format( self.id, self.name,self.description,self.image)
return str
orderdetails = db.Table('orderdetails',
db.Column('order_id', db.Integer,db.ForeignKey('orders.id'), nullable=False),
db.Column('product_id',db.Integer,db.ForeignKey('products.id'),nullable=False),
db.PrimaryKeyConstraint('order_id', 'product_id') )
class Product(db.Model):
__tablename__='products'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64),nullable=False)
description = db.Column(db.String(500), nullable=False)
image = db.Column(db.String(60), nullable=False)
price = db.Column(db.Float, nullable=False)
date = db.Column(db.DateTime, nullable=False)
stock_id = db.Column(db.Integer, db.ForeignKey('stocks.id'))
def __repr__(self):
str = "Id: {}, Name: {}, Description: {}, Image: {}, Price: {}, Stock: {}, Date: {}\n"
str =str.format( self.id, self.name,self.description,self.image, self.price, self.stock_id, self.date)
return str
class Order(db.Model):
__tablename__='orders'
id = db.Column(db.Integer, primary_key=True)
status = db.Column(db.Boolean, default=False)
firstname = db.Column(db.String(64))
phone = db.Column(db.String(32))
email = db.Column(db.String(128))
address = db.Column(db.String(128))
postcode = db.Column(db.String(32))
city = db.Column(db.String(32))
state = db.Column(db.String(32))
totalcost = db.Column(db.Float)
date = db.Column(db.DateTime)
products = db.relationship("Product", secondary=orderdetails, backref="orders")
def __repr__(self):
str = "id: {}, Status: {}, Firstname: {}, Phone: {}, Email: {}, Address: {}, Postcode: {}, City: {}, State: {}, Date: {}, Products: {}, Total Cost: {}\n"
str =str.format( self.id, self.status,self.firstname,self.phone, self.email, self.address, self.postcode, self.city, self.state, self.date, self.products, self.totalcost)
return str