Py学习  »  Django

Django更新数据后如何重定向回列表页?

boyenec • 3 年前 • 1625 次点击  

我正在列表视图中使用paginator。每页列出10项。如果我编辑了第5页的一个项目,那么我想重定向回第5页。如果我从第5页编辑一个团队,它每次都会在第1页重定向我。这是我的代码:

class ApproveCommentPage(PermissionRequiredMixin,UpdateView):

      raise_exception = True

      permission_required = ("blog.add_commentblog","blog.change_commentblog","blog.delete_commentblog","blog.view_commentblog")

      model = BlogComment

      template_name = "approve_comment.html"

      form_class =AdminComment
             

      def get_success_url(self):

                    return reverse_lazy('blog-admin') #when using this success_url my comment updated and redirected to first page.

#更新的问题。这是我的列表视图

class CommentList(PermissionRequiredMixin,ListView):
      raise_exception = True
      permission_required = ("blog.add_commentblog","blog.change_commentblog","blog.delete_commentblog","blog.view_commentblog")
      model =BlogComment
      template_name ="admin.html"
      paginate_by = 10
      ordering =["-blog_id"]

我的 网址。py

 path('blog-admin/',CommentList.as_view(),name='blog-admin'), #this is the list page where I listed my comment.
 path('blog-admin/comment-approve/<int:pk>/',ApproveCommentPage.as_view(),name='comment-approve') #this is the edit page where I am editing the comment

管理html 我在管理员中列出我的所有评论。html。每一页都有10条评论。html

我的管理员。html看起来像这样 enter image description here

这是我的密码 管理html

<form action="{% url 'comment-approve' pk=blogcomment.pk %}?page={{ page_obj.number }}">{% csrf_token %}approve {{ blogcomment.name }}
<input type="submit" value="{{ blogcomment.name }}"></form>

<!-- Pagination-->
<ul class="pagination justify-content-center mb-4">
    {% if page_obj.has_previous %}
    <li class="page-item"><a class="page-link" href="?page=1">First Page</a></li>
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">← Back</a></li>
    {% endif %}

    {% if page_obj.has_next %}
    <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next Page →</a></li>
    
    {% endif %}

    {% for i in paginator.page_range %}
    {% if page_obj.number == i %}
    <li class="page-item"><a class="page-link" href="#!">{{ i }}</a></li>
      
    {% elif i > page_obj.number|add:'-3' and i < page_obj.number|add:'3' %}
    <li class="page-item"><a class="page-link" href="?page={{ i }}">{{ i }}</a></li>
    {%endif%}
    {% endfor %}

    <li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Last Page</a></li>
</ul>
<!-- Pagination-->

#我的 通过通讯。html 我正在编辑评论:

<form  method="POST" >
            {% csrf_token %}
            {{form.as_p}}
            
            
            <button class="btn btn-info">Publish</button>
        </form>

#更新问题 加上这个 <form action="{% url 'blog-admin' %}?page={{ page_obj.number }}" method="post"> 在“我的批准”中,在“我的更新”视图中显示commnet html和以下代码:

def get_success_url(self):
        res = reverse('blog-admin')
        if 'page' in self.request.GET:
            res += f"?page={self.request.GET['page']}"
        return res

我明白了 HTTP错误405 当点击更新按钮时。看到图片了吗 enter image description here

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/127941
文章 [ 1 ]  |  最新文章 3 年前
Willem Van Onsem
Reply   •   1 楼
Willem Van Onsem    4 年前

在你的 <form> 您可以附加当前页面,例如:

<form action="{% url 'name-of-some-url' %}?page={{ page_obj.number }}" method="post">
    …
</form>

然后我们可以重定向到正确的页面:

from django.urls import reverse

class ApproveCommentPage(PermissionRequiredMixin,UpdateView):
    # …

    def get_success_url(self):
        res = reverse('blog-admin')
        if 'page' in self.request.GET:
            res += f"?page={self.request.GET['page']}"
        return res