[Answered ]-Django view with list (order and pagination) and Delete-function?

1👍

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.

1👍

in my views.py

def ProjectDetail(request,pk):
    context = {}
    template = 'views/projectdetail.html'
    project = ''
    prev = Project.objects.filter(pk__lt=pk).order_by('-pk')[:1]
   next = Project.objects.filter(pk__gt=pk).order_by('pk')[:1]
try:
    print(prev[0].pk)
    print(next[0].pk)
except:
    pass
project = Project.objects.filter(pk=pk)
context['categories'] = ProjectCategory.objects.all()
paginator = Paginator(project, 1) # Show 25 contacts per page

page = request.GET.get('page')
try:
    data = paginator.page(page)
except PageNotAnInteger:
    # If page is not an integer, deliver first page.
    data = paginator.page(1)
except EmptyPage:
    # If page is out of range (e.g. 9999), deliver last page of results.
    data = paginator.page(paginator.num_pages)
if prev:
    context['prev'] = prev[0].pk
if next:
    context['next'] = next[0].pk
context['data'] = data
return render_to_response(template, context, context_instance=RequestContext(request))

in my template i have

 <div class="row">
      <a {% if next %} href="{% url 'task:project-detail' next %}"    class="btn btn-primary pull-right" {% else %} class="btn btn-primary pull-right disabled" {% endif %}>Next</a>



    <a {% if prev %} href="{% url 'task:project-detail' prev %}" class="btn btn-primary pull-left" {% else %} class="btn btn-primary pull-left disabled" {% endif %} >Previous</a>

    </div>

Leave a comment