Answer the question
In order to leave comments, you need to log in
I can't figure out why the post request is not working?
I don’t understand why the post request on the form of the
models model does not work:
from django.db import models
from shop.models import Product
# Create your models here.
class Order(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
parent_name = models.CharField(max_length=50)
email = models.EmailField()
number = models.CharField(max_length=25)
address = models.CharField(max_length=250)
zip_code = models.CharField(max_length=20)
country = models.CharField(max_length=100)
town = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
paid = models.BooleanField(default=False)
close = models.BooleanField(default=False)
class Meta:
ordering = ('-created',)
def __str__(self):
return 'Заказ{}'.format(self.id)
def get_total_cost(self):
return sum(item.get_cost() for item in self.items.all())
class OrderItem(models.Model):
order = models.ForeignKey(Order, related_name='items', on_delete=models.CASCADE)
product = models.ForeignKey(Product, related_name='order_items', on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
quantity = models.PositiveIntegerField(default=1)
def __str__(self):
return '{}'.format(self.id)
def get_cost(self):
return self.price * self.quantity
from django import forms
from .models import Order
class OrderCreateForm(forms.ModelForm):
class Meta:
model = Order
fields = ['first_name', 'last_name', 'parent_name', 'email', 'number', 'address', 'country', 'town', 'zip_code']
from django.shortcuts import render
from .models import OrderItem
from .forms import OrderCreateForm
from cart.cart import Cart
# Create your views here.
def order_create(request):
cart = Cart(request)
form = OrderCreateForm()
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
order = form.save()
for item in cart:
OrderItem.objects.create(order=order,product=item['product'],price=item['price'],quantity=item['quantity'])
cart.clear()
return render(request, 'orders/created.html', {'order': order})
return render(request, 'orders/checkout-page.html', {'cart': cart, 'form':form})
"POST /orders/create/ HTTP/1.1" 200 21010
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question