Next and Before Links for a django paginated query

前端 未结 4 698
小蘑菇
小蘑菇 2021-01-07 06:07

I\'m trying to make a search form for Django.

Its a typical search form and then returns a table of matches. I wish to paginate the tables returned.

The prob

相关标签:
4条回答
  • 2021-01-07 06:49

    You can use {{ request.get_full_path }} template tag

    <a href="{{ request.get_full_path }}&page={{ agent_list.next_page_number }}">Next</a>

    0 讨论(0)
  • 2021-01-07 07:03

    I would recommend putting the solution in a template tag like so:

    myapp/templatetags/mytemplatetags.py:

    from django import template
    register = template.Library()
    
    @register.simple_tag
    def url_replace(request, field, value):
        d = request.GET.copy()
        d[field] = value
        return d.urlencode()
    
    @register.simple_tag
    def url_delete(request, field):
        d = request.GET.copy()
        del d[field]
        return d.urlencode()
    

    Then from templates do:

    {% load mytemplatetags %}
    ...
    <a href="?{% url_replace request 'page' agent_list.previous_page_number %}">previous</a>
    
    0 讨论(0)
  • 2021-01-07 07:03

    you can use {{ request.get_full_path }} this tag to get current url.

    <a href="{{ request.get_full_path }}&page={{ agent_list.next_page_number }}">Next</a>
    

    this worked for me

    0 讨论(0)
  • 2021-01-07 07:09

    The below works before and after a search form has been submitted:

    Views.py

    class PostListView(ListView):
        model = Post #.objects.select_related().all()
        template_name = 'erf24/home.html'  # <app>/<model>_<viewtype>.html
        context_object_name = 'posts'      # default >> erf24/post_list.html
        ordering = ['date_posted']
        paginate_by = 3
    
    def is_valid_queryparam(param):
        return param != '' and param is not None
    def invalid_queryparam(param):
        return param == '' and param is None
    
    class SearchView(ListView):
        model = Post #.objects.select_related().all()
        template_name = 'erf24/home.html'  # <app>/<model>_<viewtype>.html
        context_object_name = 'posts'      # default >> erf24/post_list.html
        ordering = ['date_posted']
        paginate_by = 3
    
        def get_queryset(self): # new
            key = self.request.GET.get('key')
            minp = self.request.GET.get('min')
            maxp = self.request.GET.get('max')
    
            if is_valid_queryparam(key):
                obj = Post.objects.filter(Q(content__icontains=key) | Q(location__icontains=key)).distinct().order_by('date_posted')
    
            if is_valid_queryparam(minp):
                obj = Post.objects.filter(Q(price__gte=minp)).distinct().order_by('date_posted')
            if is_valid_queryparam(maxp):
                obj = Post.objects.filter(Q(price__lte=maxp)).distinct().order_by('date_posted')
    
            if is_valid_queryparam(minp) & is_valid_queryparam(maxp):
                obj = Post.objects.filter(Q(price__gte=minp) & Q(price__lte=maxp)).distinct().order_by('date_posted')
    
            if is_valid_queryparam(key) & is_valid_queryparam(minp) & is_valid_queryparam(maxp):
                obj = Post.objects.filter(Q(content__icontains=key) | Q(location__icontains=key)).distinct()
                obj = obj.filter(Q(price__gte=minp) & Q(price__lte=maxp)).order_by('date_posted')
    
            if invalid_queryparam(key) & invalid_queryparam(minp) & invalid_queryparam(maxp):
                obj = Post.objects.all()
    
            return obj
    

    url.py

    urlpatterns = [
        path('', PostListView.as_view(), name='erf24-home'),
        path('search/', SearchView.as_view(), name='erf24-search'),
    ]
    

    Home.html

    <form action="{% url 'erf24-search' %}" method="GET">
        <div class="form-group">
            <label for="inputAddress">Search keyword</label>
            <input type="text" class="form-control" id="key" name="key" placeholder="keyword">
        </div>
    
        <label for="">Price</label>
        <div class="form-row">
            <div class="form-group col-md-6">
                <input type="number" class="form-control" id="min" name="min" placeholder="min price">
            </div>
            <div class="form-group col-md-6">
                <input type="number" class="form-control" id="max" name="max" placeholder="max price">
            </div>
        </div>
    
        <button type="submit" class="btn btn-primary btn-sm mt-1 mb-1">Search</button>
        <button type="reset" class="btn btn-secondary btn-sm mt-1 mb-1">Clear</button>
    
    </form>
    
    {% for post in posts %}
    <article class="media content-section">
        <div class="media-body">
            <div class="article-metadata">
                <img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}" alt="">
                <a href="{% url 'user-posts' post.author.username %}" class="mr-2">{{ post.author }}</a>
                <small class="text-muted">{{ post.date_posted }}</small>
                <!-- use |date: "specs" to filter date display -->
            </div>
            <h2>
                <a href="{% url 'post-detail' post.id %}" class="article-title">{{ post.price }}</a>
            </h2>
            <p class="article-content">{{ post.content }}</p>
            <p class="article-content">{{ post.location }}</p>
            <p><a class="like-btn" data-href="{{ post.get_api_like_url }}" href="">{{ post.likes.count }}
                    {% if user in post.likes.all %} Unlike
                    {% else %} Like
                    {% endif %}
                </a></p>
        </div>
        {% for image in post.image_set.all %}
        <img class="account-img" src="{{ image.image.url }}" alt="">
        {% endfor %}
    </article>
    {% endfor %}
    
    {% if is_paginated %}
    {% if page_obj.has_previous %}
    <a href="{% if request.GET.key is not None %}{{ request.get_full_path }}&{% else %}?{% endif %}page=1" class="btn btn-outline-info mb-4">First</a>
    <a href="{% if request.GET.key is not None %}{{ request.get_full_path }}&{% else %}?{% endif %}page={{ page_obj.previous_page_number }}" class="btn btn-outline-info mb-4">Previous</a>
    {% endif %}
    
    {% for num in page_obj.paginator.page_range %}
    {% if page_obj.number == num %}
    <a href="{% if request.GET.key is not None %}{{ request.get_full_path }}&{% else %}?{% endif %}page={{ num }}" class="btn btn-info mb-4">{{ num }}</a>
    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
    <a href="{% if request.GET.key is not None %}{{ request.get_full_path }}&{% else %}?{% endif %}page={{ num }}" class="btn btn-outline-info mb-4">{{ num }}</a>
    {% endif %}
    {% endfor %}
    
    {% if page_obj.has_next %}
    <a href="{% if request.GET.key is not None %}{{ request.get_full_path }}&{% else %}?{% endif %}page={{ page_obj.next_page_number }}" class="btn btn-outline-info mb-4">Next</a>
    <a href="{% if request.GET.key is not None %}{{ request.get_full_path }}&{% else %}?{% endif %}page={{ page_obj.paginator.num_pages }}" class="btn btn-outline-info mb-4">Last</a>
    {% endif %}
    {% endif %}
    

    Worked like a charm :) Enjoy!

    0 讨论(0)
提交回复
热议问题