Я хочу добавить и отредактировать в том же шаблоне. URL-адрес, показывающий параметры, но идентификатор не отображающий их в моих представлениях.
http://127.0.0.1: 8000 / edit_supplierdetails / 1
В моем шаблоне:
{% for supplier in suppliers %}
<a href="{% url 'edit_supplierdetails' supplier.id %}">Edit</a>
В моем URL
url(r'supplierdetails/', views.add_supplierdetails, name='add_supplierdetails'),
url(r'^edit_supplierdetails/(?P<id>\d+)$', views.add_supplierdetails, name='edit_supplierdetails'),
В моем просмотре:
def add_supplierdetails(request, id=None):
print(id)
brands = SupplierDetails.objects.all()
if id:
productcategory = get_object_or_404(SupplierDetails, id=id)
title = 'Update'
else:
productcategory = SupplierDetails()
title = 'Add'
form = SupplierDetailsForm(request.POST or None, request.FILES or None, instance=productcategory)
if request.POST and form.is_valid():
form.save()
messages.success(request, "Brand "+ title +" successfully")
return redirect('/brand')
return render(request, 'products/add_supplier2.html', {
'form': form,
'suppliers': brands,
'title': title
})