我已经看到了几个相同的问题,但是我仍然不知道如何做到这一点。我有显示超链接的模板,这些超链接指向要更新的实际记录。我想在模板上放置一个按钮,该按钮仅显示列表,从而可以通过循环遍历此“临时”表中的每个记录来在表中创建记录。该行为是为了模仿电子商务网站上的购物车。这就是为什么我将记录存储在此临时表中的原因,因为当它们“签出”时,它们将被视为多行1顺序。
在视图中编写逻辑不是问题,我不理解按下按钮时调用此函数。谢谢!
我为单击提交按钮时想要发生的行为写了一些伪代码
def process_basket(request):
basket = table1
dbtable = table2
for each record in basket:
create new record in dbtable
delete record in basket after
refresh page to show empty list
basket.html
{% load static %}
{% if user_list %}
<table>
{% for created_req in user_list %}
<tr>
<td>
<a href="{% url 'requisition:basket_item' created_req.id %}">{{ created_req }}</a>
</td>
</tr>
{% endfor %}
</table>
<form action="{% url 'requisition:process_basket' %}" method="POST">
{% csrf_token %}
<input type="button" value="Submit Req">
</form>
{% else %}
<p>No requisitions in basket</p>
{% endif %}
urls.py
path('basket/', views.basket_list, name='basket'),
path('basket/<int:id>/', views.update_basket_item, name='basket_item'),
path('basket/process/', views.process_basket, name='process_basket')
views.py
def process_basket(request):
if request.method == 'POST':
user = CurrentUser.objects.get(username=request.user)
basket_list = ReqBasket.objects.filter(user=user)
# Make decision as to set the status depending on what flag is ticked
# Assitance, Submitted
cur_status = RequisitionStatus.objects.get(status='Created')
header = Requisition.objects.create(username=user, status=cur_status)
for line in len(basket_list):
req_line = RequisitionLine.objects.create(
parent_req = header,
sequence = line,
description = basket_list.description,
extra_information=basket_list.extra_information,
quantity = basket_list.quantity,
price = basket_list.price,
req_delivery_date = basket_list.req_delivery_date)
return redirect(reverse('requisition:submitted_reqs'))
else:
return redirect(reverse('requisition:basket'))
答案 0 :(得分:0)
根据您上面的说法(模仿购物车),我认为您的解决方案更多是前端解决方案。使用Javascript和浏览器的Sessions或Localstorage API。现在该进行结帐了,您现在可以将其发送到服务器,这就是Django到位的时间。