I have a simple CRUD application with following model, List View and DetailView, where ListView is paginated by 5 items per page.
I want to put a back button on DetailView such that it will take me back to the page of ListView from which DetailView was accessed instead of going to the first page.
Similarly from DeleteView after success, I want to come back to the page in ListView where the deleted object was instead of going to first page.
The code is below.
Model
class BankAcType(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
name = models.CharField(max_length=100, verbose_name="Bank Account Type")
created_date = models.DateTimeField(default=timezone.now)
active = models.BooleanField(default=True)
def __str__(self):
return self.name
ListView
class BankAcType_List(generic.ListView):
paginate_by = 5
def get_queryset(self):
return BankAcType.objects.filter(author=self.request.user).order_by('-created_date')
DetailView
class BankAcType_Detail(DetailView):
model = BankAcType
urls.py
path('bankactype_list/', views.BankAcType_List.as_view(), name='bankactype_list'),
path('bankactype_detail/<int:pk>/', views.BankAcType_Detail.as_view(), name='bankactype_detail'),
DetailView Template
{% extends 'base_admin.html' %}
{% block content %}
</br></br>
<h2 style="text-align: center;">Bank Account Type Detail View</h2>
</br>
<div style="text-align: right; width: 60%; margin-left: auto;margin-right: auto;">
<a href="{% url 'bankactype_delete' pk=bankactype.pk %}" style="float: right; "><img src="/static/images/delete.png" alt="Edit" width="30" height="30"></a> <a href="{% url 'bankactype_edit' pk=bankactype.pk %}" style="float: right; "><img src="/static/images/edit.png" alt="Edit" width="27" height="30"></a>
</br></br>
<table class="table-bordered" style="width: 100%; padding: 2px; margin-left: auto;margin-right: auto;" >
<tr>
<td style="background-color: #000000; color: #ffffff; padding-right: 10px;">Bank Account Type</td>
<td style="background-color: #ffffff; color: #000000; text-align: center;">{{ bankactype.name }}</td>
</tr>
<tr><td style="background-color: #ffffff;"></td><td style="background-color: #000000;"></td></tr>
<tr>
<td style="background-color: #000000; color: #ffffff;padding-right: 10px;">Status</td>
<td style="background-color: #ffffff; color: #000000; text-align: center;">{{ bankactype.active|yesno:"Active, Inactive" }}</td>
</tr>
</table>
</div>
</br></br>
<div style="text-align: right; width: 60%; margin-left: auto;margin-right: auto;">
<a href="{% url 'bankactype_list' %}"><button style="background-color: #42c8f5; box-shadow: 10px 10px 25px -7px rgba(60,60,214,1.00); font-weight: bold; color: #054d05; float: left;">Back to List</button></a>
</div>
{% endblock%}