gpt4 book ai didi

python - Django 分页 - 如何重定向回 ListView 和页码

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:37 25 4
gpt4 key购买 nike

ListView 中,我使用 Paginator 和 paginate_by = 5 渲染一个表格。在每一行中,我都有一个打开 UpdateView 的按钮。成功更新后,我回到了 ListView,但始终位于第一页。如何更改 success_url 以便返回我打开 UpdateView 的页码?Django 有没有合适的方法来解决这个问题?

#views.py

class Orders_list(ListView):
model = Order
context_object_name = "orders"
queryset = Order.objects.all()
paginate_by = 5
template_name = "orders/orders_list.html"

#http://127.0.0.1:8000/orders/?page=4


class Orders_update(UpdateView):
model = Order
form_class = Order_UpdateForm
template_name = "orders/orders_update.html"
#success_url = '../../../orders/'
success_url = reverse_lazy('orders')


#urls.py

urlpatterns = [
url(r'^orders/$', Orders_list.as_view()),
url(r'^orders/detail/(?P<pk>\d+)/$', Orders_detail.as_view()),
]


#forms.py

class Order_UpdateForm(forms.ModelForm):
class Meta:
model = Order
fields = ['order_text', 'customer', 'cyclist']

def __init__(self, *args, **kwargs):
super(Order_UpdateForm, self).__init__(*args, **kwargs)
self.fields['cyclist'].queryset = Cyclist.objects.filter(active=True)


#models.py

class Order(models.Model):
customer = models.ForeignKey('Customer')
cyclist = models.ForeignKey('Cyclist')
order_text = models.CharField(max_length=200)
pick_up = models.CharField(max_length=200)
destination = models.CharField(max_length=200)
created_date = models.DateTimeField(default=timezone.now)
changed_date = models.DateTimeField(blank=True, null=True)

def created(self):
self.changed_date = timezone.now()
self.save()

def __str__(self):
return self.order_text


class Customer(models.Model):
company_name = models.CharField(max_length=200)
created_date = models.DateTimeField(default=timezone.now)
changed_date = models.DateTimeField(blank=True, null=True)

def created(self):
self.changed_date = timezone.now()
self.save()

def __str__(self): return self.company_name


class Cyclist(models.Model):
lastname = models.CharField(max_length=20)
firstname = models.CharField(max_length=20)
created_date = models.DateTimeField(default=timezone.now)
changed_date = models.DateTimeField(blank=True, null=True)
active = models.BooleanField(default=True)

def created(self):
self.changed_date = timezone.now()
self.save()

def __str__(self):
return self.lastname

更新

我找到了解决这个问题的方法(可能是)。但我还是想念一些东西。

到目前为止我做了什么:

ListView 模板中,我扩展了 Url...

window.open("{{ request.path }}update/" + id + "/?page=" + {{ page_obj.number }});

...然后在 View 中我覆盖了 get_context_data 方法...

#views.py

class Orders_update(UpdateView):
model = Order
form_class = Order_UpdateForm
template_name = "orders/orders_update.html"
page = "1" # Assigned

def get_context_data(self, **kwargs):
context = super(Orders_update, self).get_context_data(**kwargs)
page = self.request.GET.get('page') # Modified
return context

success_url = '../../../orders/?page=' + str(page) # Should be used here to redirect to a specific page on a ListView

...但是现在...如何访问 success_urlviews.py 中的变量 page ?我认为这是非常基本的东西(希望如此),而且我想我不明白作用域在 Python 中是如何工作的。

最佳答案

这最终对我有用。基本上,我只是覆盖了 get_success_url 来访问请求中的 page 参数。

class Orders_update(UpdateView):
model = Order
form_class = Order_UpdateForm
template_name = "orders/orders_update.html"

def get_success_url(self):
global page
page = self.request.GET.get('page')
return reverse_lazy('orders')

关于python - Django 分页 - 如何重定向回 ListView 和页码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40462994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com