Django将不同应用程序的多个视图传递到同一个模板

时间:2018-02-01 23:15:13

标签: python django django-templates django-views

我有一个django商店应用程序,其中所有产品都通过listview列在一个页面上(没有单独的产品页面)。但我的购物车是在不同的应用程序/模板上呈现的。将产品添加到购物车后,他们会转到另一个购物车模板。如何将视图合并为一个?提前谢谢。

购物车App views.py

from django.shortcuts import render, redirect
from catalogue.models import Product
from .models import Cart
# Create your views here.


def cart_home(request):
    cart_obj, new_obj = Cart.objects.new_or_get(request)
    return render(request, "carts/carts.html", {"cart": cart_obj})


def cart_update(request):
    product_id = request.POST.get('product_id')
    if product_id is not None:
        try:
            product_obj = Product.objects.get(id=product_id)
        except Product.DoesNotExist:
            print("Product is gone")
            return redirect("cart:home")
    cart_obj, new_obj = Cart.objects.new_or_get(request)
    if product_obj in cart_obj.products.all():
        cart_obj.products.remove(product_obj)
    else:
        cart_obj.products.add(product_obj)
    return redirect("cart:home")

这是来自Catalog app的views.py

from django.views.generic import ListView
from django.shortcuts import render
from . import models
from .models import Product
from carts.models import Cart

class ProductListView(ListView):
    model = models.Product
    template_name = "catalogue/catalogue.html"

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

        cart_obj, new_obj = Cart.objects.new_or_get(self.request)
        context['cart'] = cart_obj
        cat_appetizers = Product.objects.filter(category__name='Appetizers')
        cat_alltimefavourites = Product.objects.filter(category__name='All Time Favourites')
        cat_baltidishes = Product.objects.filter(category__name='Balti Dishes')
        cat_biryanidishes = Product.objects.filter(category__name='Biryani Dishes')
        cat_bread = Product.objects.filter(category__name='Bread')
        cat_drinks = Product.objects.filter(category__name='Drinks')
        cat_grillroastdishes = Product.objects.filter(category__name='Grill & Roast Dishes')
        cat_housespecials = Product.objects.filter(category__name='House Specials')
        cat_oldfavourites = Product.objects.filter(category__name='Old Favourites')
        cat_rice = Product.objects.filter(category__name='Rice')
        cat_setmeals = Product.objects.filter(category__name='Set Meals')
        cat_sidedishes = Product.objects.filter(category__name='Side Dishes')
        cat_sundries = Product.objects.filter(category__name='Sundries')
        cat_vegetariandishes = Product.objects.filter(category__name='Vegetarian Dishes')
        context.update({'cat_appetizers': cat_appetizers })
        context.update({'cat_alltimefavourites': cat_alltimefavourites })
        context.update({'cat_baltidishes': cat_baltidishes })
        context.update({'cat_biryanidishes': cat_biryanidishes })
        context.update({'cat_bread': cat_bread })
        context.update({'cat_drinks': cat_drinks })
        context.update({'cat_grillroastdishes': cat_grillroastdishes })
        context.update({'cat_housespecials': cat_housespecials })
        context.update({'cat_oldfavourites': cat_oldfavourites })
        context.update({'cat_rice': cat_rice })
        context.update({'cat_setmeals': cat_setmeals })
        context.update({'cat_sidedishes': cat_sidedishes })
        context.update({'cat_sundries': cat_sundries })
        context.update({'cat_vegetariandishes': cat_vegetariandishes })
        return context

0 个答案:

没有答案