1

I am creating my custom admin panel in Django, i don't want to use django default admin panel, But i want sorting filters in dajngo, If a user click on email then email should be in ascending order, and if the user click again on email then the emails will be in descending order, Please let me know how i can do it.

Here is my models.py file...

    class Model1(models.Model):
        email= models.Charfield(blank=True)
        phone= models.Charfield(blank=True)

here is my views.py file...

    def display_data(request):
        test_display=UserFilter(request.GET, 
        queryset=Model1.objects.select_related('customer__user))
        
         paginator = Paginator(test_display.qs, 10)
         page_number = request.GET.get('page')
         testdisplay = paginator.get_page(page_number)
     return render(request, 'page.html', {'test_display':test_display, 'testdisplay':testdisplay})

here is my page.html file

    <th><a href="javascript:void()">Email (CLick on this field)</a></th>
      {% for i in testdisplay %}
      <tr>
       <td>
         {{i.email}}
       </td>
      </tr>
      {% endfor %}

here is my filters.py file code...

calss UserFilter(django_filters.FilterSet):
    class Meta:
    models= Model1
    fields ['type','status']
saini tor
  • 223
  • 6
  • 21

1 Answers1

1

You can try like this:

# template
<th><a href="?order_by=email">Email (CLick on this field)</a></th>

# view
def display_data(request, id):
   test_display=Model1.objects.all()
   order_by = request.GET.get('order_by', None)
   if order_by:
       test_display = test_display.order_by(order_by)
   return render(request, 'page.html', {'test_display':test_display})

Here I am sending a URL querystring parameter order_by. In the view, it will get the querystring parameter from request.GET, and then order that queryset accordingly.

Update

Based on documentation you can add OrderingFilter:

from django_filters import FilterSet, OrderingFilter

class UserFilter(FilterSet):
   order_by = OrderingFilter(fields=(('email','email')))
   # rest of the code

And update the link in HTML:

<a onClick="window.location.href=window.location.pathname+'?order_by=email&'+window.location.search.substring(1)">

I am using inline JavaScript to resolve this issue, you can write a proper function for this.

ruddra
  • 50,746
  • 7
  • 78
  • 101
  • it's giving me this error...`UserFilter` object has no attribute `order_by`...I am using pagination also...If required please let me know I will submit my `UserFilter` code also – saini tor Sep 08 '20 at 10:13
  • Please check my updated question, i have updated the `filters.py` file and pagination also...I have multiple fields in my `filters.py` file... – saini tor Sep 08 '20 at 10:26
  • Could you please update your answer using pagination also... – saini tor Sep 08 '20 at 10:52
  • The code itself should not cause any issue witn pagination. Do you have any issue with your current pagination? If there is an erro, please share the error with stacktrace. Thanks – ruddra Sep 08 '20 at 11:21
  • this is working perfect, but when I click to `next page` it's replaced with pagination URL..for eg- my current url after ordering is `127.0.0.1/customer/?order_by=email`, but when i click on `page 2` using pagination, then it's replacing with `127.0.0.1/customer/?page=2`, but it should be `127.0.0.1/customer/?order_by=email&page=2` – saini tor Sep 08 '20 at 11:36
  • Please solve this pagination issue, I will accept your answer... – saini tor Sep 08 '20 at 12:06
  • Basically you need javascript to resolve this issue. I have added an inline solution in the answer – ruddra Sep 08 '20 at 12:12
  • Could you please solve this issue also, i want to display `Model2` data using `Model1`...https://stackoverflow.com/questions/63781362/how-to-display-foreignkey-data-in-django-html-page – saini tor Sep 08 '20 at 14:00