Django错误重定向

时间:2017-04-08 18:46:11

标签: python django django-models

无法分配“HttpResponseRedirect status_code = 302,”text / html; charset = utf-8“,url =”/ carts /“”:“Order.cart”必须是“Carts”实例“。这个错误在django中意味着什么?django的新请求帮助

def get_object(self, *args, **kwargs):
    cart_id = self.request.session.get("cart_id")
    if cart_id == None:
        return redirect("cart")
    cart = Carts.objects.get(id = cart_id)
    return cart


def get(self, request, *args, **kwargs):
    get_data = super(CheckoutView, self).get(request, *args, **kwargs)
    cart = self.get_object()
    user_checkout_id = request.session.get("user_checkout_id")
    if user_checkout_id != None:
        user_checkout = UserCheckout.objects.get(id = user_checkout_id)
        billing_address_id = request.session.get("billing_address_id")
        shipping_address_id =  request.session.get("shipping_address_id")
        if billing_address_id == None or shipping_address_id == None:
            return redirect("order_address")
        else:
            billing_address = UserAddress.objects.get(id=billing_address_id)
            shipping_address = UserAddress.objects.get(id=shipping_address_id)
        try:
            new_order_id = request.session["order_id"]
            new_order = Order.objects.get(id=new_order_id)
        except:
            new_order = Order()
            request.session["order_id"] = new_order.id
        new_order = Order()
        new_order.cart = cart
        new_order.user = user_checkout
        new_order.billing_address = billing_address
        new_order.shipping_address = shipping_address
        new_order.save()

    return redirect("checkout")

class Order(models.Model):

cart = models.ForeignKey(Carts)
user =  models.ForeignKey(UserCheckout, null=True)
shipping_address = models.ForeignKey(UserAddress, related_name='shipping_address', null=True)
billing_address = models.ForeignKey(UserAddress, related_name='billing_address', null=True)
shipping_total_price = models.DecimalField(default=5.99, decimal_places=2, max_digits=50)
order_total = models.DecimalField( decimal_places=2, max_digits=50)


def __str__(self):
    return self.carts.id

类Carts(models.Model):

user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
items =  models.ManyToManyField(Variation, through=CartItem)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
tax_percentage = models.DecimalField(max_digits=10, decimal_places=5, default=0.085)
subtotal = models.DecimalField(max_digits=50, decimal_places=2, default=250.00)
tax_total = models.DecimalField(max_digits=50, decimal_places=2, default=250.00)
total = models.DecimalField(max_digits=50, decimal_places=2, default=250.00)


def __str__(self):
    return str(self.id)

0 个答案:

没有答案