简体   繁体   English

如何在Django中“添加到购物车”模型对象

[英]How to add model objects “to cart” in django

I'm trying to work out how to use a very simple bit of "shopping cart" functionality, but I'm no good at this. 我正在尝试找出如何使用非常简单的“购物车”功能,但是我对此并不擅长。

Let's say I have a simple queryset, "objectos" that comes from: 假设我有一个简单的查询集“ objectos”,它来自:

# models.py:
class ObjectModel(models.Model):
    title = models.CharField(max_length=30)
    ...
    content = models.TextField()


# views.py
def product_list(request):
    objectos = ObjectModel.objects.all()
    return render(request, 'frontpage/object_list.html', {'objectos':objectos})

..And let's say I need to insert a simple button in the template that grabs an object from the queryset and puts the object in another queryset. 假设我需要在模板中插入一个简单的按钮,该按钮从queryset抓取一个对象,然后将该对象放入另一个queryset。 This is what I can't figure out; 这是我不知道的。 how to do that secondary queryset.. 如何执行该次查询集。

Anyway, here's a simplified look at what I'm trying to do in the template: 无论如何,这是我要在模板中尝试做的简化视图:

<!-- template -->
{% for obj in objectos %}
  {{ obj.title }}
  {{ obj.content }}
  <a href="#">
    <button type="button">add to other qs!</button>
  </a>
{% endfor %}

How could this be done in a simple fashion? 如何以一种简单的方式做到这一点?

I'm considering building a shopping cart, only a lot simpler, but I'm not sure that's the right way to approach it. 我正在考虑构建一个购物车,只是简单得多,但是我不确定这是否是正确的方法。 If you coulddirect me to a good shopping cart tutorial or similar that I could look at, I'd be most grateful :) 如果您能指导我找到一个不错的购物车教程或类似的产品,我将不胜感激:)

There are a lot of ways you can do this, but the best way would be to follow a tutorial which has easy steps to follow and explains throughout the way. 您可以通过多种方法来执行此操作,但是最好的方法是遵循一个教程,该教程具有易于遵循的步骤并在整个过程中进行了说明。 I did some research, and this looks like a great tutorial which has multiple pages, each one explaining a different part of building a simple shopping cart. 我做了一些研究,这看起来像是一篇很棒的教程,其中包含多个页面,每个页面解释了构建简单购物车的不同部分。

https://blog.muva.tech/lesson-1-building-e-commerce-shopping-cart-using-django-2-0-python-3-6/ https://blog.muva.tech/lesson-1-building-e-commerce-shopping-cart-using-django-2-0-python-3-6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM