将GET参数传递给href链接

时间:2014-09-22 07:44:00

标签: python django

我有疑问,不知道如何表达以找到答案

当我去:http://127.0.0.1:8000/image/test/?title="test"
然后想要转到另一个页面,它将转到:http://127.0.0.1:8000/image/test/?page=2" 我知道应该去:http://127.0.0.1:8000/image/test/?title="test&page=2"
但不知道该怎么做 如何编辑url以传递参数(title =“test)以转到正确的URL?

我还有关于网址的第二个问题

如果我现在正在路上

http://127.0.0.1:8000/image/test/?title="test

如何编写绝对href以转到http://127.0.0.1:8000/image/test/

请帮助我谢谢

test.html:

<form action="" method="get"  class="searchtitle">
      search title:<input type="text" name="title">
        <button type="submit" value="submit" >search</button>
</form>
...
<div class="pagination">
<span class="step-links">
    {% if contacts.has_previous %}
        <a href="?page={{ contacts.previous_page_number }}">previous</a>
    {% endif %}
    <span class="current">
        Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
    </span>
    {% if contacts.has_next %}
        <a href="?page={{ contacts.next_page_number }}">next</a>
    {% endif %}
    <form action="" method="get">
         <td>GO to</td>
         <td><input name="page" type="text" ></td>
         td> page </td>
         <td><input type="submit" value=" go to page "></td>
         </form>
</span>

views.py:

def object_list_1(request, model):
    if request.GET.get("title", None):
        search_term = request.GET['title']
        cls = get_model('mongo', model)
        obj_list = cls.objects.filter(title__contains=search_term)   
        results = get_paginator(request, obj_list, 10)
        template_name = 'filterimgs/%s_list.html' % model.lower()
        return render_to_response(template_name, {'object_list': obj_list,'contacts': results},
                          context_instance=RequestContext(request))

1 个答案:

答案 0 :(得分:2)

问题在于<a href="?page={{ contacts.previous_page_number }}">;在这里,您忽略任何查询字符串(?title="test"部分)。

要解决此问题,您需要先enable the request template context processor,然后更新模板,使其包含完整的网址,如下所示:

<a href="{{ request.get_full_path }}&page={{ contact.previous_page_number }}">