Как перенаправить возврат на ту же страницу после POST в этом случае. django
Я создаю этот проект на django. Я работаю над записью Add the number of products by BARCOD , где я перехожу к продукту -detail view, затем нажимаю на ссылку продукта. Добавляю количество товара в форму Все работает хорошо, но есть одна вещь. После добавления количества товаров по BARCOD я попадаю на случайную(?) страницу ...., но я хотел бы остаться на той же странице Добавить количество товара в форме. Но как??? views
def returnitm_PK(request,item_PK_id):
clientR=ClientPK.objects.get(place=request.POST.get("clientR"))
item_PK=StockPK.objects.get(pk=item_PK_id)
quan_BARCOD=request.POST.get("quan_BARCOD")
Ear_number=request.POST.get("Ear_number")
transaction=TransactionPK(item_PK=item_PK,clientR=clientR,quan_BARCOD=quan_BARCOD,Ear_number=Ear_number)
transaction.save()
if quan_BARCOD== '0009478908' :
item_PK.R1=item_PK.R1+int(1)
if quan_BARCOD== '0005663140' :
item_PK.R2=item_PK.R2+int(1)
item_PK.save()
context = {'transaction':transaction,'item_PK':item_PK,'clientR':clientR,
'quan_BARCOD':quan_BARCOD,'Ear_number':Ear_number, }
return render(request, "details_PK", context)
урлы
path('F_add_items/',views.F_add_items,name='F_add_items.html'),
path('F_list_history/', views.F_list_history, name='F_list_history.html'),
path('F_list_item/', views.F_list_item, name='F_list_item'),
path('<int:item_PK_id>/details',views.details_PK,name='details_PK'),
path('<int:item_id>/',views.details,name='details'),
path('<int:item_id>/transfer',views.transferitm,name='transferitm'),
path('<int:item_id>/returnitm',views.returnitm,name='returnitm'),
###############################################################################
path('F_add_PK/',views.F_add_PK,name='F_add_PK.html'),
path('F_list_history_PK/', views.F_list_history_PK, name='F_list_history_PK.html'),
path('F_list_item_PK/', views.F_list_item_PK, name='F_list_item_PK'),
path('<int:item_PK_id>/returnitm_PK',views.returnitm_PK,name='returnitm_PK'),
details_PK
{% extends 'base.html' %}
{% block content %}
<center>
<div class="card" style="width: 50rem;">
<div class="card-body">
<form action="returnitm_PK" method="POST" >
<div class="card-header">add {{ item.name }}</div>
{% csrf_token %}
<input type="hidden" name="item" value="{{ item.name }}">
<div class="form-group">
<label for="clientR">clientR</label>
<select name="clientR" class="form-control" id="client">
{% for clientR in ClientPKs %}
<option value="{{ clientR.place }}">{{ clientR.place }}</option>
{% endfor %}
</select>
</div>
<!--
<div class="form-group">
<label for="Ear_number">رقم الاذن</label>
<input type="text" class="form-control" value="0" id="Ear_number" name="Ear_number" min="1"/>
</div>
-->
<div class="form-group">
<label for="quan_BARCOD">quan_BARCOD</label>
<input type="number" class="form-control" value="" id="quan_BARCOD" name="quan_BARCOD" min="0" max="9999999999"/>
</div>
<input type="submit" class="btn btn-primary" value="add">
</form>
</div>
</div>
</center>
{% endblock %}
F_list_history_PK
{% extends 'base.html' %}
{% block content %}
<script type="text/javascript">
$(document).ready(function() {
$('#example').DataTable();
} );
</script>
<table id="example" class="table table-bordered display" style="width:100%">
<thead class="thead-dark">
<tr>
<th>COUNT</th>
<th>num</th>
<th>category</th>
<th>BARCOD</th>
<th>28</th>
<th>30</th>
<th>32</th>
<th>34</th>
<th>36</th>
<th>38</th>
<th>color</th>
<th>TIMESTAMP</th>
</tr>
</thead>
<tbody>
{% for item in items_list %}
<tr>
<td>{{ item.item_PK_id }}</td>
<td><a href="{% url 'details_PK' item.item_PK_id %}">{{ item.name }}</a></td>
<td>{{ item.category }}</td>
<td>{{ item.BARCOD }}</td>
<td>{{ item.R1 }}</td>
<td>{{ item.R2 }}</td>
<td>{{ item.R3 }}</td>
<td>{{ item.R4 }}</td>
<td>{{ item.R5 }}</td>
<td>{{ item.R6 }}</td>
<td>{{ item.color }}</td>
<td>{{ item.timestamp }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<th>COUNT</th>
<th>num</th>
<th>category</th>
<th>BARCOD</th>
<th>R1</th>
<th>R2</th>
<th>R3</th>
<th>R4</th>
<th>R5</th>
<th>R6</th>
<th>color</th>
<th>TIMESTAMP</th>
</tr>
</tfoot>
</table>
{% endblock %}
Непонятно, что именно вы имеете в виду. К какому представлению вы хотите перейти в каждом случае? В общем, с подобным можно поступить следующим образом:
def returnitm_PK(request,item_PK_id):
clientR=ClientPK.objects.get(place=request.POST.get("clientR"))
item_PK=StockPK.objects.get(pk=item_PK_id)
if request.method == 'POST':
quan_BARCOD=request.POST.get("quan_BARCOD")
Ear_number=request.POST.get("Ear_number")
transaction=TransactionPK(item_PK=item_PK,clientR=clientR,quan_BARCOD=quan_BARCOD,Ear_number=Ear_number)
transaction.save()
if quan_BARCOD== '0009478908' :
item_PK.R1=item_PK.R1+int(1)
if quan_BARCOD== '0005663140' :
item_PK.R2=item_PK.R2+int(1)
item_PK.save()
return redirect('name_of_view_you_want_to_redirect_to_CAN_be_same_page')
context = {'transaction':transaction,'item_PK':item_PK,'clientR':clientR,
'quan_BARCOD':quan_BARCOD,'Ear_number':Ear_number, }
return render(request, "details_PK", context)