cannot change list attribute of current_user in flask
Question:
I want to create a cart for my website. Here is my User
class:
class User(UserMixin, db.Model):
__tablename__ = 'users'
user_id = db.Column(db.Text, primary_key=True)
password = db.Column(db.Text)
roles = db.Column(db.String(length=25))
stall_id = db.Column(db.Integer, db.ForeignKey('stalls.stall_id'), nullable=True)
cart = []
def __init__(self, user_id, password, role):
self.user_id = user_id
self.password = password
self.roles = role
def get_id(self):
return self.user_id
The user can add items to the cart, I am using UserMixin
and current_user
to access the cart of the user. Everything works fine here.
@app.route('/add-to-cart/<int:picked_dish_id>', methods=['GET', 'POST'])
@login_required
def add_to_cart(picked_dish_id):
dish = Dish.query.filter_by(dish_id=picked_dish_id).first()
current_user.cart.append(dish)
print(current_user.cart)
return render_template('add_to_cart.html', dish=dish)
But I have problems with deleting the item from the cart, here is my function
@app.route('/delete-from-cart/<int:dish_id>')
@login_required
def delete_from_cart(dish_id):
print(current_user.cart)
dishes = current_user.cart
current_user.cart = [dish for dish in dishes if dish.dish_id != int(dish_id)]
print("Cart after deleting ", end='')
print(current_user.cart)
return redirect(url_for('checkout'))
After this function is called I am rendering the checkout page again to update the changes
@app.route('/checkout')
@login_required
def checkout():
print("Cart in checkout: ", end='')
print(current_user.cart)
return render_template('checkout.html', cart=current_user.cart)
So when I am pressing delete button in my front-end firstly delete_from_cart
function and after that it redirects user to checkout
page to update the changes.
So here is the output of this two functions when I am trying to delete the item from the cart
[<Dish 1>, <Dish 4>]
Cart after deleting [<Dish 4>]
Cart in checkout: [<Dish 1>, <Dish 4>]
As you can see the function actually deletes the item from the cart and in function delete_from_cart
current_user.cart
list is different, but when it redirects to the checkout page the changes disappear.
Answers:
I dont know but .remove()
function works completely fine and now I can remove items from the cart now
@app.route('/delete-from-cart/<int:dish_id>', methods=['POST'])
@login_required
def delete_from_cart(dish_id):
for dish in current_user.cart:
if dish.dish_id == dish_id:
current_dish = dish
current_user.cart.remove(current_dish)
return redirect(url_for('checkout'))
I want to create a cart for my website. Here is my User
class:
class User(UserMixin, db.Model):
__tablename__ = 'users'
user_id = db.Column(db.Text, primary_key=True)
password = db.Column(db.Text)
roles = db.Column(db.String(length=25))
stall_id = db.Column(db.Integer, db.ForeignKey('stalls.stall_id'), nullable=True)
cart = []
def __init__(self, user_id, password, role):
self.user_id = user_id
self.password = password
self.roles = role
def get_id(self):
return self.user_id
The user can add items to the cart, I am using UserMixin
and current_user
to access the cart of the user. Everything works fine here.
@app.route('/add-to-cart/<int:picked_dish_id>', methods=['GET', 'POST'])
@login_required
def add_to_cart(picked_dish_id):
dish = Dish.query.filter_by(dish_id=picked_dish_id).first()
current_user.cart.append(dish)
print(current_user.cart)
return render_template('add_to_cart.html', dish=dish)
But I have problems with deleting the item from the cart, here is my function
@app.route('/delete-from-cart/<int:dish_id>')
@login_required
def delete_from_cart(dish_id):
print(current_user.cart)
dishes = current_user.cart
current_user.cart = [dish for dish in dishes if dish.dish_id != int(dish_id)]
print("Cart after deleting ", end='')
print(current_user.cart)
return redirect(url_for('checkout'))
After this function is called I am rendering the checkout page again to update the changes
@app.route('/checkout')
@login_required
def checkout():
print("Cart in checkout: ", end='')
print(current_user.cart)
return render_template('checkout.html', cart=current_user.cart)
So when I am pressing delete button in my front-end firstly delete_from_cart
function and after that it redirects user to checkout
page to update the changes.
So here is the output of this two functions when I am trying to delete the item from the cart
[<Dish 1>, <Dish 4>]
Cart after deleting [<Dish 4>]
Cart in checkout: [<Dish 1>, <Dish 4>]
As you can see the function actually deletes the item from the cart and in function delete_from_cart
current_user.cart
list is different, but when it redirects to the checkout page the changes disappear.
I dont know but .remove()
function works completely fine and now I can remove items from the cart now
@app.route('/delete-from-cart/<int:dish_id>', methods=['POST'])
@login_required
def delete_from_cart(dish_id):
for dish in current_user.cart:
if dish.dish_id == dish_id:
current_dish = dish
current_user.cart.remove(current_dish)
return redirect(url_for('checkout'))