Skip to content Skip to sidebar Skip to footer

Django 1.6: Displaying A Particular Models Objects In Another Template

Working on an application were I have a One to Many relationship where I have many Products and a few particular products will be related to only one Website. On my Home page is wh

Solution 1:

You can add this in product_extend urls.py:

urlpatterns = patterns('',
    url(r'^(?P<website_slug>[\w]+)$', ProductView.as_view(), name='products_list'),
)

Then in ProductView override the get_queryset method to use the website_slug for filtering the queryset:

classProductView(ListView):

    context_object_name = 'product_list'
    template_name = 'product_extend/_productlist.html'# queryset = ProductExtend.objects.filter(id=1)
    model = Product

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

    defget_queryset(self):
        qs = super(ProductView, self).get_queryset()
        return qs.filter(website__website_slug__exact=self.kwargs['website_slug'])

Solution 2:

after reading twice, think what you want is:

url(r'^product/website/(?P<slug>)$', "your_view_to_peform_product_search_for_slug_website_here"),

and in your view "HTML"

href="product/website/{{ website.slug }}"

something like this...

Post a Comment for "Django 1.6: Displaying A Particular Models Objects In Another Template"