Answer the question
In order to leave comments, you need to log in
How to submit a list through the form, in one of the lines of which there is a list?
- the client adds product IDs to the session!
- in the cart, edits the products withdrawn from the session and proceeds to checkout, where he fills in his data and completes the order, which saves the order in the database! To place an order, I use the form of the Order model in which one field (products from the client session) is hidden and it has several products (list), so I can’t pass this list through the form, but only the last element ...
- request.POST .getlist("name_product"), as I understand it, should be used to get the specified list, but how to fasten it?
models.py
class Order(models.Model):
first_name = models.CharField(u"Имя заказчика", max_length= 100)
second_name = models.CharField(u"Фамилия заказчика", max_length= 100)
phone = models.CharField(u"Контактный телефон заказчика", max_length= 100)
email = models.EmailField(u"Электронная почта")
address = models.CharField(u"Адрес доставки", max_length= 100)
name_product = models.CharField(u"Товары заказа", max_length= 100)
def __iter__(self):
return[self.first_name]
def __str__(self):
return self.first_name
class Meta:
verbose_name="Заказ"
verbose_name_plural="Заказы"
forms.py
class OrderForm(ModelForm):
class Meta:
model = Order
fields = ["first_name","second_name",
"phone","email","address",
"form_shipping","form_payment",
"name_product"
]
views.py
def order(request):
cart = request.session.get('cart')
product_items = Product.objects.filter(id__in=cart)
#беру товары из сессии
if request.method == "POST":
form = OrderForm(request.POST)
request.POST.getlist("name_product") #нужно где-то прикрутить
if form.is_valid():
instance = form.save(commit=False)
instance.save()
return redirect("/")
else:
form = OrderForm()
content = {
"product_items":product_items
}
return render (request, "order.html", content)
order.html
<form action="" method="POST">{% csrf_token %}
<label for="id_first_name">Имя заказчика:</label> <input id="id_first_name" maxlength="100" name="first_name" type="text" />
<label for="id_second_name">Фамилия заказчика:</label> <input id="id_second_name" maxlength="100" name="second_name" type="text" />
<label for="id_phone">Контактный телефон заказчика:</label> <input id="id_phone" maxlength="100" name="phone" type="text" />
<label for="id_email">Электронная почта:</label> <input id="id_email" maxlength="254" name="email" type="email" />
<label for="id_address">Адрес доставки:</label> <input id="id_address" maxlength="100" name="address" type="text" />
{% for product_item in product_items %}
<input type='hidden' name="name_product" value="{{ product_item }}"/> ВОТ ЗДЕСЬ ЗАГВОЗДКА!!!!
{% endfor%}
<input type="submit" value="Завершить заказ!" />
</form>
Answer the question
In order to leave comments, you need to log in
You can create a field for each product in the form constructor:
class OrderForm(ModelForm):
def __init__(self, request, *args, **kwargs):
super(OrderForm, self).__init__(*args, **kwargs)
cart = request.session.get('cart')
product_items = Product.objects.filter(id__in=cart)
for product in product_items:
key = 'product_{index}'.format(index=product.pk)
self.fields[key] = forms.BooleanField(required=False) # тут поле какое тебе удобней
def clean(self):
# здесь обрабатываешь поля из self.cleaned_data
return self.cleaned_data
you can use this field for the form https://gist.github.com/aklim007/c325753b3e171adb8674
just to validate a previously unknown list of objects of a given type.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question