So as you can see on a page I have Shop name and it's adress. Moreover I have an edit function on views.py:
def update_shop(request, id):
context = {}
# * fetch the object related to passed id
obj_shop = get_object_or_404(VideoLibrary, id=id)
# * pass the object as instance in form
form_shop = VideoLibraryForm(request.POST or None, instance=obj_shop)
# * save the data from the form and
# * redirect to detail_view
if form_shop.is_valid():
form_shop.save()
return HttpResponseRedirect('/main/')
context['form_shop'] = form_shop
return render(request, 'update/update_shop.html', context)
urls.py:
...
path('list_view/', views.list_view, name='list_view'),
path('shop/<id>/update', views.update_shop, name='update_shop'),
The image is on a different page ('lilst_view') and the issue is - I need to place a button on this page and redirect to the update page in order to edit certain shop names for example. To do this as I suppose I need to get somehow the id of the object. Does anybody know how to do it?
list_view.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div >
{% for data in library_dataset %} Shop name:
<br />
<br />
{{ data.shop_name }} <br />
{{ data.adress }} <br />
{% endfor %}
</div>
</body>
</html>
views.py (def list_view):
def list_view(request):
context = {}
context['library_dataset'] = VideoLibrary.objects.all()
return render(request, 'app/list_view.html', context)
CodePudding user response:
#list_view.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div >
{% for data in library_dataset %}
Shop name:
<br />
<br />
{{ data.shop_name }} <br />
{{ data.adress }} <br />
<td><a href="{% url 'update_shop' data.id %}">
<button type="button" >Edit Shop Details</button>
</a>
</td>
{% endfor %}
</div>
</body>
</html>
CodePudding user response:
in this case you can do it in Django by simply useing redirect() method with parameters, for example
return redirect('some-view-name', parameter_name='par_value')
and in a tag put <a href={% url 'view_name' desired_id %}