Django视图与列表(顺序和分页)和删除功能? [英] Django view with list (order and pagination) and Delete-function?

查看:153
本文介绍了Django视图与列表(顺序和分页)和删除功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html模板,我列出了Django模型对象。此页面还有一个分页和链接来排序对象。现在我正在考虑在每个对象上放置一个删除按钮,它从列表和数据库中删除它,而不会破坏顺序或分页(返回当前的查询集而不删除对象)。



我已经尝试使用自己的url的删除视图,但我认为最好的办法是把它放在同一个视图中,以便能够返回/处理order_by和pagination。像我的分页链接一样加载href,如:



mylist /?page = {{results.next_page_number}}& order_by = {{order_by}} / p>

但是还有一个附加的delete-someID参数(什么是首选方法?)。我不知道这是干净(和安全?)的方法吗?我正在考虑检查对象用户对登录的用户。

  deleteid = get_object_or_404(Mymodel,pk =?) #在这里做什么? 
如果deleteid.user == request.user:
deleteid.delete()



<我正在想这里?我需要一些指导,把所有这一切放在一起。



查看:

  def Mylist(request):

order_by = request.GET.get('order_by','somedefault')
myobjects_list = Mymodel.objects.filter(user = request.user)。 order_by(order_by)
paginator = Paginator(myobjects_list,5)

page = request.GET.get('page')
try:
results = paginator。页面(页)
除了PageNotAnInteger:
results = paginator.page(1)
除了EmptyPage:
results = paginator.page(paginator.num_pages)

context = {'results':results,'order_by':order_by}
return render_to_response('mylist.html',context,context_instance = RequestContext(request))


解决方案

这是我将如何做:


  1. 发出一个django视图的ajax调用,以删除像/ entery / 3 / delete这样的URL的条目,

  2. 该视图将检查验证,执行删除并返回一个简单的成功/失败
    标志(也许在json左边),

  3. 在客户端,执行-on success - 一个java脚本,从当前页面隐藏该条目。 (即设置样式:显示为无或删除HTML元素)。

这样我就可以获得快速响应,减少网络流量不需要,更好的用户体验和更高的安全性。



希望这有帮助。


I have a html-template where I list users Django model objects. This page also have a pagination and links to order the objects. Now I'm thinking of putting a delete-button on each object, which deletes it from the list and database without breaking the order or pagination (returning the current queryset without the deleted object).

I've tried with a delete-view with it's own url but I think the best would be to put it all in the same view to be able to return/handle the order_by and pagination. Something like my pagination links that loads a href like:

"mylist/?page={{ results.next_page_number }}&order_by={{ order_by }}"

But with an additional delete-someID-parameter (what's the preferred method?). I'm not sure if this is the clean (and secure?) way to do this? I'm thinking of checking the objects user against the logged in user also.

deleteid = get_object_or_404(Mymodel, pk=?) #what to do here?
if deleteid.user == request.user:
    deleteid.delete()

I'm thinking right here? I need some guidance to put all this together.

View:

def Mylist(request):

    order_by = request.GET.get('order_by', 'somedefault')   
    myobjects_list = Mymodel.objects.filter(user=request.user).order_by(order_by)
    paginator = Paginator(myobjects_list, 5)

    page = request.GET.get('page')
    try:
        results = paginator.page(page)
    except PageNotAnInteger:
        results = paginator.page(1)
    except EmptyPage:
        results = paginator.page(paginator.num_pages)

    context = {'results ': results, 'order_by': order_by }
    return render_to_response('mylist.html', context, context_instance=RequestContext(request))

解决方案

Here is how i would do it:

  1. Issue an ajax call to a django view to delete the entry on a url like /entery/3/delete ,
  2. that view would check validation , execute delete and return a simple success/failure flag (maybe in json or so),
  3. on the client side, execute -on success- a java script to hide that entry from the current page. (ie. Set style:display to None or delete the HTML element).

That way i get fast response , less network traffic that isn't needed , a better user experience and a heightened security.

Hope this helps.

这篇关于Django视图与列表(顺序和分页)和删除功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆