0

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>&nbsp;&nbsp;<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%}
Parry
  • 177
  • 3
  • 16

1 Answers1

0

I was able to do it the following way.

First find the position of the object in the list and then use it to get a page number on the list in the view and pass it in context to the page. (See View Below)

Next in the template use the page number received from the view to build url for the back button. (See Template below)

View

class BankAcType_Detail(DetailView):
    model = BankAcType
    pk = BankAcType.pk

    def get_context_data(self, **kwargs):
        context = super(BankAcType_Detail, self).get_context_data(**kwargs)
        pk = self.kwargs['pk']
        blist = BankAcType.objects.filter(author=self.request.user).values_list('pk', flat=True).order_by('-created_date')
        position = list(blist).index(pk)
        if position == 0:
            page = "1"
        else:
            page = math.floor(position/5)+1
        context ['page'] = page
        return context

Template

<a href="/master/bankactype_list/?page={{page}}">Back</a>
Parry
  • 177
  • 3
  • 16