无法在django视图中获取url中的GET表单数据

时间:2017-08-26 08:10:50

标签: django django-templates django-views

我正在尝试制作搜索视图,并显示结果。所以对于没有分页的搜索视图工作正常但是当我添加分页时它停止工作。以下是我的代码

views.py

def search_query(request):
    if request.method == 'GET':
        #try:
        if 'q' in request.GET:
            query_string= request.GET.get('q')

            if len(query_string) == 0:
                return HttpResponseBadRequest('Invalid Search')
            else:
                query_list = query_string.split(' ')
                posts = _search(query_list)
                paginator = Paginator(posts, 1) 
                page = request.GET.get('page')
                if page is None:
                    page = 1
                else:
                    pass
                try:
                    posts = paginator.page(page)

                except PageNotAnInteger:

                    posts = paginator.page(1)
                    return HttpResponseBadRequest()
                except EmptyPage:

                    posts = paginator.page(paginator.num_pages)

                    return HttpResponseBadRequest()

                context = {
                    'posts': posts,
                }
                return render(request, 'search/search.html', {'posts': posts,})
    #except:
        #   return HttpResponseBadRequest('Outside Try')

以下功能进行实际搜索

def _search(query_list):
    result = []
    for query in query_list:
        query = query.lower()
    try:        
        posts = Post.objects.order_by('-date_created').filter(tag__tag__icontains=query)
        for post in posts:
            if post in result:
                pass
            else:
                result.append(post)
    except:
        result = None
    return result
下面的

是search.html分页部分的片段

<nav aria-label="Page navigation example">
        <ul class="pagination justify-content-center">

        {% if posts.has_previous %}
            {% comment %} <a href="?page={{ posts.previous_page_number }}">previous</a> {% endcomment %}
            <li class="page-item">
                <a class="page-link" href="?q={{ request.GET.q }}&?page={{ posts.previous_page_number }}" tabindex="-1">Previous</a>
            </li>
        {% else %}
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1">Previous</a>
            </li>
        {% endif %}

          <li class="page-item"><a class="page-link" href="?q={{ request.GET.q }}&?page={{ posts.number }}">{{ posts.number }}</a></li>

        {% if posts.has_next %}
            <li class="page-item">
                <a class="page-link" href="?q={{ request.GET.q }}&?page={{ posts.next_page_number }}">Next</a>
            </li>
        {% endif %}
        </ul>
    </nav>

现在问题是当我打印(页面)

时页面的值是无
page = request.GET.get('page')

2 个答案:

答案 0 :(得分:0)

你可以这样做吗

{{1}}
来自 query_params

包含您传递的内容

答案 1 :(得分:0)

  

当页面是第二个参数

时,无需在?之前添加page

用这个改变代码

<nav aria-label="Page navigation example">
        <ul class="pagination justify-content-center">

        {% if posts.has_previous %}
            {% comment %} <a href="?page={{ posts.previous_page_number }}">previous</a> {% endcomment %}
            <li class="page-item">
                <a class="page-link" href="?q={{ request.GET.q }}&page={{ posts.previous_page_number }}" tabindex="-1">Previous</a>
            </li>
        {% else %}
            <li class="page-item disabled">
                <a class="page-link" href="#" tabindex="-1">Previous</a>
            </li>
        {% endif %}

          <li class="page-item"><a class="page-link" href="?q={{ request.GET.q }}&page={{ posts.number }}">{{ posts.number }}</a></li>

        {% if posts.has_next %}
            <li class="page-item">
                <a class="page-link" href="?q={{ request.GET.q }}&page={{ posts.next_page_number }}">Next</a>
            </li>
        {% endif %}
        </ul>
    </nav>