question about variables

19 views
Skip to first unread message

Ronny Abraham

unread,
Apr 18, 2014, 9:25:21 AM4/18/14
to djang...@googlegroups.com
I'm working on implementing django-shop, and I'm having a few problems, one of those is that on every page I want it to display at the top infomration about the shopping cart, e.g. it should say:

"Your Cart: 5 items, $35.68 total"


Now, I do not know how to pass the cart information without creating my own modified views for every django-shop page.   I could do something ugly, like modify the urls and create my own views.  e.g.

urls.py:
   import myapp.views as mycustomviews

    url(r'^catalog/(?P<path>.+)/product/(?P<slug>[0-9A-Za-z-_.//]+)/$',
        mycustomviews.CustomProductView.as_view(),
        name='product_detail'
        ),
    (r'^catalog/', include('shop_categories.urls')),


myapp/views.py:

class CustomProductView(CategoryProductDetailView):

    def get_context_data(self, **kwargs):
        context = super(ProductView, self).get_context_data(**kwargs)

        mycart = # figure out how to get the session cart info and put it here
        context["mycart"] = mycart

        return cont



I REALLY don't want to do this.  This is my first shop, and the less I mess with the underlying code, the happier I am.  Is there some way I can either access the cart information from my templates, or alternatives pass that information along?

This is actually a general problem for me, there's always information I want to pass along to the templates, and I have no idea how to do it without overloading the views in django-shop


Caleb Njoroge

unread,
Apr 20, 2014, 3:22:15 PM4/20/14
to djang...@googlegroups.com
hello +Ronny Abraham 

  django-shop does include a template tag for that will help you with you problem, you ca take a look at the django-shop template tags here https://github.com/divio/django-shop/blob/master/shop/templatetags/shop_tags.py#L17,

you can include this within a block in your base.html, and it will be supplied to all your templates, this is also a good idea for other similar problems. if you want to pass a common context variable to all your template, you can take a look at the context processor, https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS

cheers 
Muhia
Reply all
Reply to author
Forward
0 new messages