Domen Preložnik
Domen Preložnik

Reputation: 348

Django: redirect user two pages back

I have a list of multiple objects from my database (named "plp's"), arranged in a table. Next to each "plp" element I have a button "Edit" to modify that particular entry.

Next, I redirect the user to a new url, where I pass the id of that "plp", and show the form to edit it, with a "save" button.

After pressing the "save", which is request.POST, I want to redirect the user back to the first url, with the list of all the "plp" objects in one list. That means to the site, where he first pressed "Edit".

Can I somehow save the url of where the "Edit" was clicked, and pass it to my views.py?

Thank you

listdns.html:
<td>
<a href='{% url "plpuredi" plp_id=pos.id %}' class="btn btn-primary btn-sm">Uredi</a>
</td>

urls.py:

rl(r'^(?P<plp_id>\d+)/uredi$', plp_list_uredi,name="plpuredi")

views.py:

def plp_list_uredi(request, plp_id=None):
moj_plp=PLPPostavka.objects.get(id=plp_id)
form=PLPPostavkaForm(request.POST or None,request=request,dns=moj_plp.dns, instance=moj_plp)
context ={
    'plp':moj_plp,
    'form':form,
}
if request.POST:
    if form.is_valid():
        plp = form.save()
        return redirect(request.path)
return render(request, "plp_pos/uredi.html",context)

uredi.html

<form action="" method="POST">
{% csrf_token %}
<div class="box">
<div class="box-header">
    <h4 class="box-title">
       Urejanje PLP Postavke
    </h4>
</div>
<div class="box-body">
{% for field in form %}
<div class="form-group">
<label for="{{ field.id_for_label }}" class="col-md-2 control-label detail">{{ field.label }}</label>
<div class="col-md-10">
{% if field|field_type == "datefield" %}
  {% render_field field class+="form-control dateinput" %}
{% else %}
  {% render_field field class+="form-control" %}
{% endif %}       
</div>
</div>
{% endfor %}
</div>
<div class="box-footer">
    <div class="box-tools pull-right">
        <input type="submit" value="Shrani" class="btn btn-primary" />
    </div>
</div>

Upvotes: 1

Views: 1479

Answers (1)

Pavel Isaev
Pavel Isaev

Reputation: 73

Don't you only have 1 page to edit all the elements? Then you could perhaps hardcode the link e.g.

return HttpResponseRedirect(my_edit_url)

If this doesn't work and you need to go 2 pages back take a look at this post: How to redirect to previous page in Django after POST request

Upvotes: 1

Related Questions