You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django分页ListView:从DetailView/DeleteView返回原页面实现

Alright, let's fix this so your back buttons take users exactly where they came from instead of dumping them on the first page. Here's how to do it step by step:

1. Pass the current page number from ListView to DetailView

First, you need to send the active page number when users click into a detail page. Open your ListView template (probably bankactype_list.html) and update the detail link to include the page parameter:

<!-- In your ListView template's loop over objects -->
<a href="{% url 'bankactype_detail' pk=object.pk %}?page={{ page_obj.number }}">{{ object.name }}</a>

This grabs the current page number from the paginator's page_obj and appends it to the detail URL.

2. Update DetailView to pass the page number to its template

Modify your BankAcType_Detail view to capture the page parameter and send it to the template:

class BankAcType_Detail(DetailView):
    model = BankAcType

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        # Get the page number from the request, default to 1 if not present
        context['page_number'] = self.request.GET.get('page', 1)
        return context

Now update the back button in your detail template to use the passed page number:

<a href="{% url 'bankactype_list' %}?page={{ page_number }}">
    <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>

Also, update the delete link in the same template to carry the page number over to the DeleteView:

<a href="{% url 'bankactype_delete' pk=bankactype.pk %}?page={{ page_number }}" style="float: right; ">
    <img src="/static/images/delete.png" alt="Delete" width="30" height="30">
</a>

4. Create/Update DeleteView to redirect to the correct page

If you don't have a BankAcType_Delete view yet, add it. We'll override get_success_url to handle the page redirection, including adjusting the page if we just deleted the last item on the current page:

from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy

class BankAcType_Delete(DeleteView):
    model = BankAcType

    def get_success_url(self):
        # Get the original page number from the request
        page = self.request.GET.get('page', 1)
        
        # Calculate the new total pages after deletion
        user_accounts = BankAcType.objects.filter(author=self.request.user)
        new_count = user_accounts.count() - 1  # Subtract the deleted item
        paginate_by = 5  # Match your ListView's paginate_by
        
        if new_count > 0:
            new_total_pages = (new_count + paginate_by - 1) // paginate_by
            # If the original page is now beyond the total, go to the last page
            if int(page) > new_total_pages:
                page = new_total_pages
        else:
            # No items left, go to page 1
            page = 1
        
        # Build the URL with the correct page
        return f"{reverse_lazy('bankactype_list')}?page={page}"

5. Add DeleteView to urls.py

Don't forget to add the DeleteView path to your urls.py:

path('bankactype_delete/<int:pk>/', views.BankAcType_Delete.as_view(), name='bankactype_delete'),

That's it! Now when users click back from the detail page, they'll land right on the page they were viewing. And after deleting an item, they'll either stay on the same page (if there are still items left) or go to the previous page (if they deleted the last item on the page).

内容的提问来源于stack exchange,提问作者Parry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 10:47:35