如何与CreateView一起发送一些参数,然后在模板中访问它们?

时间:2017-01-17 13:29:50

标签: django django-models django-forms django-templates django-views

我正在制作一个在线商店类型的网站,并且我无法使我的添加到购物车选项才能正常工作。我还没有将其余代码链接到按钮,并且正在使用另一个链接来操作它,正如您在代码中看到的那样。我希望表单自动提交项目名称和品牌。请提出一些建议。

urls.py

url(r'^(?P<pk>[0-9]+)/addtocart/$', views.ItemAdd.as_view(), name='addtocart'),

models.py

class Mycart(models.Model):
    name = models.CharField(max_length=250)
    brand = models.CharField(max_length=250)
    quantity = models.IntegerField(default='1')

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('products:detail', kwargs={'pk': self.pk})

views.py

class ItemAdd(CreateView):
    model = Mycart
    fields = ['name', 'brand', 'quantity']
    template_name = 'products/add_to_cart.html'

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

add_to_cart.html

{% extends 'products/base.html' %} {% block body %}

<form action="" method="post" enctype="multipart/form-data">
  {% csrf_token %}
  <input type="hidden" name="name" value="{{ object.name }}">
  <input type="hidden" name="brand" value="{{ object.brand }}">
  <br>
  <p>Enter Quantity</p>
  <input type="number" name="quantity" value="">
  <button type="submit" class="btn btn-success">Submit</button>

</form>

{% endblock %}

2 个答案:

答案 0 :(得分:0)

据我所知,当用户点击商品(产品)时,您需要自动添加名称和品牌到表格,因此用户只需输入数量并提交表格?也许你可以尝试这样:

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        product_pk = self.kwargs['pk']
        product = Product.objects.get(pk=product_pk)
        context.update({
            'product': product
        })
        return context

现在,您可以访问模板中的产品并获取名称和品牌:

   {{ product.name }}
   {{ product.brand }}

答案 1 :(得分:0)

您可以使用Formview。然后你会有:

models.py

class Cart(models.Model):
    quantity = models.PositiveIntegerField()
    product = models.ForeignKey('products.Product')

forms.py

class AddCartForm(forms.ModelForm):
    def save(self, product):
        instance = super(AddCartForm, self).save(commit=False)
        instance.product = product
        instance.save()
        return instance

    class Meta:
        model = Cart
        fields = '__all__'

views.py

class AddCartView(FormView):
    form_class = AddCartForm
    success_url = '/'

    def dispatch(self, request, *args, **kwargs):
        product_pk = kwargs.get('product_pk')
        self.product = get_object_or_404(Product, pk=product_pk)
        return super(
            AddCartView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kw):
        context = super(AddCartView, self).get_context_data(**kw)
        context.update(product=self.product)
        return context

    def form_valid(self, form):
        form.save(product=self.product)
        return super(AddCartView, self).form_valid(form)

add_cart.html

{% extends 'products/base.html' %} {% block body %}

<form action="{% url 'cart:add' product.pk %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<button type="submit" class="btn btn-success">Submit</button>
</form>

{% endblock %}