我的表单不向db django发送数据
我的表单不向数据库发送数据。 我有一个表单,在输入数据并单击“订单”后,应将信息发送到数据库。然而,这并没有发生 它是一个“购物车”,发送数据后,应将用户提供的信息以及订购产品的数据保存在订单表中 购物车/视图.py我的表单不向db django发送数据,django,python-3.x,django-models,django-templates,django-views,Django,Python 3.x,Django Models,Django Templates,Django Views,我的表单不向数据库发送数据。 我有一个表单,在输入数据并单击“订单”后,应将信息发送到数据库。然而,这并没有发生 它是一个“购物车”,发送数据后,应将用户提供的信息以及订购产品的数据保存在订单表中 购物车/视图.py def cart_detail(request, total=0, counter=0, cart_items = None): try: cart = Cart.objects.get(cart_id=_cart_id(request)) cart_i
def cart_detail(request, total=0, counter=0, cart_items = None):
try:
cart = Cart.objects.get(cart_id=_cart_id(request))
cart_items = CartItem.objects.filter(cart=cart, active=True)
for cart_item in cart_items:
total += (cart_item.product.price * cart_item.quantity)
counter += cart_item.quantity
except ObjectDoesNotExist:
pass
if request.method == 'POST':
total = request.POST['total']
billingName = request.POST['billingName']
billingAddress1 = request.POST['billingAddress1']
billingCity = request.POST['billingCity']
billingPostcode = request.POST['billingPostcode']
billingCountry = request.POST['billingCountry']
shippingName = request.POST['shippingName']
shippingAddress1 = request.POST['shippingAddress1']
shippingCity = request.POST['shippingCity']
shippingPostcode = request.POST['shippingPostcode']
shippingCountry = request.POST['shippingCountry']
order_details = Order.objects.create(total=total, billingName=billingName, billingAddress1=billingAddress1, billingCity=billingCity, billingPostcode=billingPostcode,billingCountry=billingCountry, shippingName=shippingName, shippingAddress1=shippingAddress1, shippingCity=shippingCity, shippingPostcode=shippingPostcode, shippingCountry=shippingCountry)
order_details.save()
for order_item in cart_items:
oi = OrderItem.objects.create(
product = order_item.product.name,
quantity = order_item.quantity,
price = order_item.product.price,
order = order_details
)
oi.save()
'''Reduce stock when order is placed or saved'''
products = Product.objects.get(id=order_item.product.id)
products.stock = int(order_item.product.stock - order_item.quantity)
products.save()
order_item.delete()
'''The terminal will print this message when the order is saved'''
print('The order has been created')
return redirect('cart:cart_detail')
return render(request, 'cart/cart.html', dict(cart_items = cart_items, total = total, counter = counter))
from django.db import models
class Order(models.Model):
total = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='GBP Order Total')
created = models.DateTimeField(auto_now_add=True)
billingName = models.CharField(max_length=250, blank=True)
billingAddress1 = models.CharField(max_length=250, blank=True)
billingCity = models.CharField(max_length=250, blank=True)
billingPostcode = models.CharField(max_length=10, blank=True)
billingCountry = models.CharField(max_length=200, blank=True)
shippingName = models.CharField(max_length=250, blank=True)
shippingAddress1 = models.CharField(max_length=250, blank=True)
shippingCity = models.CharField(max_length=250, blank=True)
shippingPostcode = models.CharField(max_length=10, blank=True)
shippingCountry = models.CharField(max_length=200, blank=True)
class Meta:
db_table = 'Order'
ordering = ['-created']
def __str__(self):
return str(self.id)
class OrderItem(models.Model):
product = models.CharField(max_length=250)
quantity = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='GBP Price')
order = models.ForeignKey(Order, on_delete=models.CASCADE)
class Meta:
db_table = 'OrderItem'
def sub_total(self):
return self.quantity * self.price
def __str__(self):
return self.product
app_name='cart'
urlpatterns = [
path('add/<int:product_id>/', views.add_cart, name='add_cart'),
path('', views.cart_detail, name='cart_detail'),
path('remove/<int:product_id>/', views.cart_remove, name='cart_remove'),
path('full_remove/<int:product_id>/', views.full_remove, name='full_remove'),
]
订单/型号.py
def cart_detail(request, total=0, counter=0, cart_items = None):
try:
cart = Cart.objects.get(cart_id=_cart_id(request))
cart_items = CartItem.objects.filter(cart=cart, active=True)
for cart_item in cart_items:
total += (cart_item.product.price * cart_item.quantity)
counter += cart_item.quantity
except ObjectDoesNotExist:
pass
if request.method == 'POST':
total = request.POST['total']
billingName = request.POST['billingName']
billingAddress1 = request.POST['billingAddress1']
billingCity = request.POST['billingCity']
billingPostcode = request.POST['billingPostcode']
billingCountry = request.POST['billingCountry']
shippingName = request.POST['shippingName']
shippingAddress1 = request.POST['shippingAddress1']
shippingCity = request.POST['shippingCity']
shippingPostcode = request.POST['shippingPostcode']
shippingCountry = request.POST['shippingCountry']
order_details = Order.objects.create(total=total, billingName=billingName, billingAddress1=billingAddress1, billingCity=billingCity, billingPostcode=billingPostcode,billingCountry=billingCountry, shippingName=shippingName, shippingAddress1=shippingAddress1, shippingCity=shippingCity, shippingPostcode=shippingPostcode, shippingCountry=shippingCountry)
order_details.save()
for order_item in cart_items:
oi = OrderItem.objects.create(
product = order_item.product.name,
quantity = order_item.quantity,
price = order_item.product.price,
order = order_details
)
oi.save()
'''Reduce stock when order is placed or saved'''
products = Product.objects.get(id=order_item.product.id)
products.stock = int(order_item.product.stock - order_item.quantity)
products.save()
order_item.delete()
'''The terminal will print this message when the order is saved'''
print('The order has been created')
return redirect('cart:cart_detail')
return render(request, 'cart/cart.html', dict(cart_items = cart_items, total = total, counter = counter))
from django.db import models
class Order(models.Model):
total = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='GBP Order Total')
created = models.DateTimeField(auto_now_add=True)
billingName = models.CharField(max_length=250, blank=True)
billingAddress1 = models.CharField(max_length=250, blank=True)
billingCity = models.CharField(max_length=250, blank=True)
billingPostcode = models.CharField(max_length=10, blank=True)
billingCountry = models.CharField(max_length=200, blank=True)
shippingName = models.CharField(max_length=250, blank=True)
shippingAddress1 = models.CharField(max_length=250, blank=True)
shippingCity = models.CharField(max_length=250, blank=True)
shippingPostcode = models.CharField(max_length=10, blank=True)
shippingCountry = models.CharField(max_length=200, blank=True)
class Meta:
db_table = 'Order'
ordering = ['-created']
def __str__(self):
return str(self.id)
class OrderItem(models.Model):
product = models.CharField(max_length=250)
quantity = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='GBP Price')
order = models.ForeignKey(Order, on_delete=models.CASCADE)
class Meta:
db_table = 'OrderItem'
def sub_total(self):
return self.quantity * self.price
def __str__(self):
return self.product
app_name='cart'
urlpatterns = [
path('add/<int:product_id>/', views.add_cart, name='add_cart'),
path('', views.cart_detail, name='cart_detail'),
path('remove/<int:product_id>/', views.cart_remove, name='cart_remove'),
path('full_remove/<int:product_id>/', views.full_remove, name='full_remove'),
]
模板/cart/cart.html
<button class="btn send-click btn-md my-1 btn-block" data-toggle="modal" data-target="#inquiryModal">Complete Order</button>
<!-- Inquiry Modal -->
<div class="modal fade" id="inquiryModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="inquiryModalLabel">Complete Order</h5>
<button type="button" class="close" data-dismiss="modal">
<span>×</span>
</button>
</div>
<div class="modal-body">
<form action="{% url 'cart:cart_detail' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="billingName" class="col-form-label">Name:</label>
<input type="text" name="billingName" class="form-control"
value="" required>
</div>
<div class="form-group">
<label for="billingAddress1" class="col-form-label">Adress:</label>
<input type="text" name="billingAddress1" class="form-control"
value="" required>
</div>
<div class="form-group">
<label for="billingCity" class="col-form-label">City:</label>
<input type="text" name="billingCity" class="form-control"
value="" required>
</div>
<div class="form-group">
<label for="billingPostcode" class="col-form-label">Post code:</label>
<input type="text" name="billingPostcode" class="form-control"
value="" required>
</div>
<div class="form-group">
<label for="billingCountry" class="col-form-label">Country:</label>
<input type="text" name="billingCountry" class="form-control"
value="" required>
</div>
<div class="form-group">
<label for="shippingName" class="col-form-label">Shop name:</label>
<input type="text" name="shippingName" class="form-control"
value="" required>
</div>
<div class="form-group">
<label for="shippingAddress1" class="col-form-label">Shop adress:</label>
<input type="text" name="shippingAddress1" class="form-control"
value="" required>
</div>
<div class="form-group">
<label for="shippingCity" class="col-form-label">Shop City:</label>
<input type="text" name="shippingCity" class="form-control"
value="" required>
</div>
<div class="form-group">
<label for="shippingPostcode" class="col-form-label">Shop Post code:</label>
<input type="text" name="shippingPostcode" class="form-control"
value="" required>
</div>
<div class="form-group">
<label for="shippingCountry" class="col-form-label">Shop Country:</label>
<input type="text" name="shippingCountry" class="form-control"
value="" required>
</div>
<hr>
<input type="submit" value="Order" class="btn send-click btn-md my-0 btn-block">
</form>
</div>
</div>
</div>
完成订单
完整命令
&时代;
{%csrf_令牌%}
姓名:
地址:
城市:
邮政编码:
国家:
店名:
店铺地址:
商店城市:
店铺邮政编码:
商店所在国:
cart/url.py
def cart_detail(request, total=0, counter=0, cart_items = None):
try:
cart = Cart.objects.get(cart_id=_cart_id(request))
cart_items = CartItem.objects.filter(cart=cart, active=True)
for cart_item in cart_items:
total += (cart_item.product.price * cart_item.quantity)
counter += cart_item.quantity
except ObjectDoesNotExist:
pass
if request.method == 'POST':
total = request.POST['total']
billingName = request.POST['billingName']
billingAddress1 = request.POST['billingAddress1']
billingCity = request.POST['billingCity']
billingPostcode = request.POST['billingPostcode']
billingCountry = request.POST['billingCountry']
shippingName = request.POST['shippingName']
shippingAddress1 = request.POST['shippingAddress1']
shippingCity = request.POST['shippingCity']
shippingPostcode = request.POST['shippingPostcode']
shippingCountry = request.POST['shippingCountry']
order_details = Order.objects.create(total=total, billingName=billingName, billingAddress1=billingAddress1, billingCity=billingCity, billingPostcode=billingPostcode,billingCountry=billingCountry, shippingName=shippingName, shippingAddress1=shippingAddress1, shippingCity=shippingCity, shippingPostcode=shippingPostcode, shippingCountry=shippingCountry)
order_details.save()
for order_item in cart_items:
oi = OrderItem.objects.create(
product = order_item.product.name,
quantity = order_item.quantity,
price = order_item.product.price,
order = order_details
)
oi.save()
'''Reduce stock when order is placed or saved'''
products = Product.objects.get(id=order_item.product.id)
products.stock = int(order_item.product.stock - order_item.quantity)
products.save()
order_item.delete()
'''The terminal will print this message when the order is saved'''
print('The order has been created')
return redirect('cart:cart_detail')
return render(request, 'cart/cart.html', dict(cart_items = cart_items, total = total, counter = counter))
from django.db import models
class Order(models.Model):
total = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='GBP Order Total')
created = models.DateTimeField(auto_now_add=True)
billingName = models.CharField(max_length=250, blank=True)
billingAddress1 = models.CharField(max_length=250, blank=True)
billingCity = models.CharField(max_length=250, blank=True)
billingPostcode = models.CharField(max_length=10, blank=True)
billingCountry = models.CharField(max_length=200, blank=True)
shippingName = models.CharField(max_length=250, blank=True)
shippingAddress1 = models.CharField(max_length=250, blank=True)
shippingCity = models.CharField(max_length=250, blank=True)
shippingPostcode = models.CharField(max_length=10, blank=True)
shippingCountry = models.CharField(max_length=200, blank=True)
class Meta:
db_table = 'Order'
ordering = ['-created']
def __str__(self):
return str(self.id)
class OrderItem(models.Model):
product = models.CharField(max_length=250)
quantity = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='GBP Price')
order = models.ForeignKey(Order, on_delete=models.CASCADE)
class Meta:
db_table = 'OrderItem'
def sub_total(self):
return self.quantity * self.price
def __str__(self):
return self.product
app_name='cart'
urlpatterns = [
path('add/<int:product_id>/', views.add_cart, name='add_cart'),
path('', views.cart_detail, name='cart_detail'),
path('remove/<int:product_id>/', views.cart_remove, name='cart_remove'),
path('full_remove/<int:product_id>/', views.full_remove, name='full_remove'),
]
app\u name='cart'
URL模式=[
路径('add/',views.add_cart,name='add_cart'),
路径(“”,views.cart\u detail,name='cart\u detail'),
路径('remove/',views.cart\u remove,name='cart\u remove'),
路径('full_remove/',views.full_remove,name='full_remove'),
]
会发生什么情况?“如果”条件可能不起作用。我没有错误信息。我已经坐了很多次了,什么都没想到。我的表单不将数据保存到数据库。我需要一个新的外观。如果request.method==“POST”:
不起作用?可能是的。因为数据没有保存。事实上,我不知道错误在哪里,但肯定在我插入的元素中。可能?你根本没有做任何调试?