I have two function in Django Templates.
def cart(request, template="shop/cart.html"):
cart_formset = CartItemFormSet(instance=request.cart)
def checkout_steps(request):
How I use the cart_formset variable in my checkout_steps function..
I have two function in Django Templates.
def cart(request, template="shop/cart.html"):
cart_formset = CartItemFormSet(instance=request.cart)
def checkout_steps(request):
How I use the cart_formset variable in my checkout_steps function..
You can't. That's what local variables are: they're local to the function they're in. The variable cart_formset
does not exist except when cart
is executing.
You could return cart_formset
from the function and then call cart
from checkout_steps
to get the value. Or you could store that variable somewhere else. I don't know enough about Django to know where the best place is. Given that what you're doing looks like a shopping cart setup, I would guess that you want to store that info with the session, so you could look at Django's session handling.
How I use the cart_formset variable in my checkout_steps function..
If the two functions are related, use a Class (like Shopping) and add cart and checkout_steps as member functions.
class Shopping(object):
def __init__(self):
self.cart_formset = None
def cart(self, request, template="shop/cart.html"):
self.cart_formset = CartItemFormSet(instance=request.cart)
def checkout_steps(self, request):
#Use self.cart_formset
You can also put the variable cart_formset, as global to make it available globally across the module
def cart(request, template="shop/cart.html"):
global cart_formset
cart_formset = CartItemFormSet(instance=request.cart)
def checkout_steps(self, request):
#Use self.cart_formset
If the order of function evaluation is deterministic then you can use a decorator