我使用Django的分页模块和基于类的视图。
我的查询集将包含1000多个对象,我需要逐个显示这些对象并根据显示的内容进行显示。
我不需要显示以前的页面,我只需要接受/拒绝,并且在两次点击时,它应该分页到下一页。
我需要根据用户点击的Accept / Reject更新数据库中的几个字段。如何在基于类的视图中从模板中获取POST请求参数?这样我就可以点击“接受/拒绝”
执行不同的步骤这是我的代码:
#views.py
from django.views.generic import ListView
class DemoView2(ListView):
model = MandateTable
template_name = 'demo2.html'
context_object_name = 'pg'
paginate_by = 1
queryset = MandateTable.objects.all()[:5] #For Testing purspose, working on only 5 objects
模板
<!--demo2.html-->
{% for mandates in pg %}
<p>{{ mandates.FIELD_1 }}</p>
<p>{{ mandates.FIELD_2 }}</p>
<p>{{ mandates.FELD_3 }}</p>
<p>{{ mandates.FIELD_4 }}</p>
<p>{{ mandates.FIELD_5 }}</p>
{% endfor %}
{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
<button value="Accept" name="btn-accept" class="btn btn-success btn-lg">
{% if page_obj.has_next %}
<a style="text-decoration: none" href="?page={{ page_obj.next_page_number }}">Accept</a>
{% else %}
<a style="text-decoration: none" href="{% url 'demo' %}">Accept</a>
{% endif %}
</button>
<button value="Reject" name="btn-reject" class="btn btn-danger btn-lg">
{% if page_obj.has_next %}
<a style="text-decoration: none" href="?page={{ page_obj.next_page_number }}">Reject</a>
{% else %}
<a style="text-decoration: none" href="{% url 'demo' %}">Reject</a>
{% endif %}
</button>
</ul>
{% endif %}
我对Pagination的基于类的视图不熟悉。
感谢所有帮助
答案 0 :(得分:0)
那么:
from django.views.generic import ListView
from django.urls import reverse
class DemoView2(ListView):
model = MandateTable
template_name = 'demo2.html'
context_object_name = 'pg'
paginate_by = 1
queryset = MandateTable.objects.all()[:5] #For Testing purspose, working on only 5 objects
def post(self, request, *args, **kwargs):
if 'accept' in request.POST:
# do acceptable stuff
pass
elif 'reject' in request.POST:
# do rejectable stuff
pass
return super().get(request, *args, **kwargs)
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
page_obj = context['page_obj']
if page_obj.has_next():
context['next'] = "?page=" + str(page_obj.next_page_number())
else:
context['next'] = reverse('demo')
return context
<!--demo2.html-->
<form method='post'>
{% csrf_token %}
{% for mandates in pg %}
<p>{{ mandates.FIELD_1 }}</p>
<p>{{ mandates.FIELD_2 }}</p>
<p>{{ mandates.FELD_3 }}</p>
<p>{{ mandates.FIELD_4 }}</p>
<p>{{ mandates.FIELD_5 }}</p>
{% endfor %}
{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
<button name='accept' class="btn btn-danger btn-lg" type="submit" formaction={{ next }}>Accept</button>
<button name='reject' class="btn btn-danger btn-lg" type="submit" formaction={{ next }}>Reject</button>
</ul>
{% endif %}
</form>
您仍然需要以某种方式将该页面上显示的对象包含到request.POST中,以便您可以对它们进行操作(可能使用隐藏的输入)。以前从未使用过分页。
答案 1 :(得分:0)
此解决方案对我有用。
main.js
$("#btn_reject").click(function () {
//Fetch few parameters and send it along with clicked button in AJAX request
var mNum = $("#mNum")[0].getAttribute('data-mn');
var data = {"btn_name": "APPROVE", "m_number": mNum};
man_verification(data);
});
$("#btn_reject").click(function () {
//Fetch few parameters and send it along with clicked button in AJAX request
var mNum = $("#mNum")[0].getAttribute('data-mn');
var data = {"btn_name": "REJECT", "m_number": mNum};
man_verification(data);
});
function man_verification(data) {
$.ajax({
type: "POST",
url: "/review/",
data: data,
success: function (data) {
//Code For Handlind Response Data
},
error: function (data) {
console.log("Error", data);
},
beforeSend: function (xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});
}
views.py
class RevPageView(ListView):
template_name = 'rev.html'
queryset = db_query
def get_context_data(self, *, object_list=None, **kwargs):
context = super(RevPageView, self).get_context_data(**kwargs)
try:
#Get data from db
paginator = Paginator(db_query, 1)
page = self.request.GET.get('page')
try:
query = paginator.page(page)
except PageNotAnInteger:
query = paginator.page(1)
except EmptyPage:
query = paginator.page(paginator.num_pages)
context['data'] = query
return context
def post(self, request, *args, **kwargs):
data_status = None
if request.is_ajax():
get_pk = request.POST.get('mandate_number')
m_status = request.POST.get('btn_name')
#Make necessary changes to the db tables based on the received parameters
data = {"data_status": data_status}
return JsonResponse(data)
如果有比这种方法更有效的方法:)