我的申请是一家餐厅的网站。人们可以订购食物。我在使用此功能时遇到问题。
我无法弄清楚如何在DB中存储订单。
models.py:
DRINK_TYPES = (
(1, 'soft_drinks'),
(2, 'drinks'),
(3, 'wines'),
(4, 'beer'),
(5, 'juice'),
)
WINE_COLORS = (
(1, 'white'),
(2, 'rose'),
(3, 'red'),
)
WINE_SWEETNESS = (
(1, 'dry'),
(2, 'sweet'),
(3, 'semi-sweet'),
(4, 'semi-dry'),
)
DRINK_VOLUME = (
(1, 'by the glass'),
(2, 'bottle'),
(3, 'decanter'),
)
class Order(models.Model):
name_surname = models.CharField(max_length=99, verbose_name='imię nazwisko')
address = models.CharField(max_length=199, verbose_name='adres')
email = models.EmailField(verbose_name='email')
phone_number = models.CharField(max_length=20, verbose_name='numer telefonu')
self_pickup = models.BooleanField(verbose_name='odbiór własny')
dish = models.ManyToManyField(Dish, verbose_name='danie')
drink = models.ManyToManyField(Drinks, verbose_name='napój')
date_added = models.DateTimeField(default=now, verbose_name='wysłane')
dish_order_quantity = models.CharField(max_length=10, verbose_name=' danie główne ilość', default=0)
drink_order_quantity = models.CharField(max_length=10, verbose_name='drink ilość', default=0)
wine_order_quantity = models.CharField(max_length=10, verbose_name='wino ilość', default=0)
juice_order_quantity = models.CharField(max_length=10, verbose_name='sok ilość', default=0)
beer_order_quantity = models.CharField(max_length=10, verbose_name='piwo ilość', default=0)
class Dish(models.Model):
dish_name = models.CharField(max_length=150, verbose_name='danie')
price = models.DecimalField(max_digits=5, decimal_places=1, verbose_name='cena')
ingredient = models.ForeignKey(Ingredient)
class Drinks(models.Model):
drink_type = models.IntegerField(choices=DRINK_TYPES, null=True, blank=True, verbose_name='napoje typ')
drink_volume = models.IntegerField(choices=DRINK_VOLUME, null=True, blank=True, verbose_name='objętość')
wine_colors = models.IntegerField(choices=WINE_COLORS, null=True, blank=True, verbose_name='kolor wina')
wine_sweetness = models.IntegerField(choices=WINE_SWEETNESS, null=True, blank=True, verbose_name='smak wina')
drink_name = models.CharField(max_length=99, null=True, blank=True, verbose_name='napoje nazwa')
price = models.DecimalField(max_digits=5, decimal_places=1, default=0.00, verbose_name='cena')
class OrderView(View):
def post(self, request):
form = OrderForm(request.POST)
if form.is_valid():
new = Order.objects.create(
dish_name=request.POST.get('dish_{{dish.id}}'),
drink=request.POST.get('drink_{{drink.id}}'),
name_surname=request.POST.get('name_surname'),
address=request.POST.get('address'),
email=request.POST.get('email'),
phone_number=request.POST.get('phone_number'),
self_pickup=request.POST.get('self_pickup')
dish_order_quantity=request.POST.get('dish_order_quantity'),
wine_order_quantity=request.POST.get('wine_order_quantity'),
drink_order_quantity=request.POST.get('drink_order_quantity'),
juice_order_quantity=request.POST.get('juice_order_quantity'),
beer_order_quantity=request.POST.get('beer_order_quantity'),
)
return redirect('order')
def get(self, request):
form = OrderForm
dishes = Dish.objects.all()
wines = Drinks.objects.filter(drink_type=3)
beers = Drinks.objects.filter(drink_type=4)
juices = Drinks.objects.filter(drink_type=5)
soft_drinks = Drinks.objects.filter(drink_type=1)
drinks = Drinks.objects.filter(drink_type=2)
return render(request, 'order.html', {'dishes': dishes,
'drinks': drinks,
'wines': wines,
'beers': beers,
'juices': juices,
'soft_drinks': soft_drinks,
'form': form})
order.html:
<div class="extendables_bottom_margin">
<form method="POST" id="order_form">
{% csrf_token %}
<div class="container">
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h1 class="panel-title">
<a data-toggle="collapse" href="#collapse1">Dania główne</a>
</h1>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">
{% for dish in dishes %}
{{ dish.dish_name }} ................. {{ dish.price }}
<div class="menu_choice">
<input name="dish_{{dish.id}}" type="checkbox" class="inline"/>
<select name="dish_order_quantity" class="menu_choice_count" form="order_form">
<option value="--">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div><br>
{% endfor %}
</div>
</div>
</div>
</div>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h1 class="panel-title">
<a data-toggle="collapse" href="#collapse2">Drinki</a>
</h1>
</div>
<div id="collapse2" class="panel-collapse collapse">
<div class="panel-body">
{% for drink in drinks %}
{{ drink.drink_name }} ................. {{ drink.price }}
<div class="menu_choice">
<input name="drink_{{drink.id}}" type="checkbox" class="inline"/>
<select name="drink_order_quantity" class="menu_choice_count" form="order_form">
<option value="--">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div><br>
{% endfor %}
</div>
</div>
</div>
</div>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h1 class="panel-title">
<a data-toggle="collapse" href="#collapse3">Wina</a>
</h1>
</div>
<div id="collapse3" class="panel-collapse collapse">
<div class="panel-body">
{% for wine in wines %}
{{ wine.drink_name }} / {{ wine.get_drink_volume_display }} ................. {{ wine.price }}
<div class="menu_choice">
<input name="wine_{{drink.id}}" type="checkbox" class="inline"/>
<select name="wine_order_quantity" class="menu_choice_count" form="order_form">
<option value="--">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div><br>
{% endfor %}
</div>
</div>
</div>
</div>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h1 class="panel-title">
<a data-toggle="collapse" href="#collapse4">Soki</a>
</h1>
</div>
<div id="collapse4" class="panel-collapse collapse">
<div class="panel-body">
{% for juice in juices %}
{{ juice.drink_name }} ................. {{ juice.price }}
<div class="menu_choice">
<input name="juice_{{drink.id}}" type="checkbox" class="inline"/>
<select name="juice_order_quantity" class="menu_choice_count" form="order_form">
<<option value="--">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div><br>
{% endfor %}
</div>
</div>
</div>
</div>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h1 class="panel-title">
<a data-toggle="collapse" href="#collapse5">Piwa</a>
</h1>
</div>
<div id="collapse5" class="panel-collapse collapse">
<div class="panel-body">
{% for beer in beers %}
{{ beer.drink_name }} ................. {{ beer.price }}
<div class="menu_choice">
<input name="beer_{{drink.id}}" type="checkbox" class="inline"/>
<select name="beer_order_quantity" class="menu_choice_count" form="order_form">
<option value="--">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div><br>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
<div class="cd">
{{ form.as_p }}
<input type="submit" value="wyślij">
</form>
</div>
在我的.html中,我有一个循环打印单个菜肴/饮料。每个循环都有一个单选按钮和一个下拉数量的订单。
1。当有人订购超过1道主菜或喝酒时,如何保存在数据库中? 我想我需要改变我的模型但不确定如何。
2。一般来说,即使我订购了1道菜和饮料,我也无法保存在数据库中,我无法弄清楚这个错误。
我很欣赏所有的意见。
谢谢。
答案 0 :(得分:0)
django可以使用request.GET.get('input_name')
读取表单数据,这将返回输入值,
如果选中该复选框,则值为True
如果你想从select输入中获取数据,请执行以下操作:
html代码
<select name = "myselect" class="menu_choice_count">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
django视图
choice = request.GET.get('myselect')
mymodel.objects.create(choice=choice...)