简体   繁体   English

如何将值传递给搜索查询

[英]How to pass a value to a search query

I am working with two pages and I would like to click on a tag on one page, which would insert a value to a search query on another page.我正在处理两个页面,我想单击一个页面上a tag ,这会在另一个页面上的搜索查询中插入一个值。

So here's my views.py:所以这是我的 views.py:

def bikes_all(request):
  item_list = Bike.objects.all()

  category_q = request.GET.get('cat')

  if category_q:
      item_list = item_list.filter(category__pk=category_q)

  paginator = Paginator(item_list, 10)

  page = request.GET.get('page')

  try:
      items = paginator.page(page)
  except PageNotAnInteger:
      items = paginator.page(1)
  except EmptyPage:
      items = paginator.page(paginator.num_pages)

  context = {
    'items': items,
  }

  return render(request, "bikes_all.html", context)

and my template:和我的模板:

            <form method="GET" action="{% url 'core:bikes_all' %}">
            <div class="form-row ">
            <div class="form-group col-5">
                <label for="category">Category</label>
                <select id="cat" class="form-control" name="cat">
                    <option value="" {% if not request.GET.cat %} selected {% endif %}>Choose...</option>
                    {% for cat in category_list %}
                    <option value="{{ cat.pk }}" {% if request.GET.cat == cat.pk|slugify %} selected {% endif %}>
                        {{ cat }}</option>
                    {% endfor %}
                </select>
            </div>
        </div>
        <div class="form-row">
            <button type="submit" class="btn btn-outline-primary btn-md">Search</button>
        </div>
    </form>

and here's the a tag from another page:这是来自另一个页面的a tag

            <div class="col-md-4 overlay zoom">
            <a href="{% url 'core:bikes_all'  %}">
                <div style="position:relative;">
                    <img src="{% static '/img/category_choice/bike33.png' %}" class="img-fluid">
                    <div class="card-img-overlay">
                        <h2 class="card-title"
                            style="text-align: center; color: aliceblue; position: absolute; bottom:5px;">
                            Road Bikes
                        </h2>
                    </div>
                </div>
            </a>
        </div>

So I have {% url 'core:bikes_all' %} in my href, which takes to bikes_all.html , but I would like to pass a search query within that href as well.所以我的 href 中有{% url 'core:bikes_all' %} ,它需要bikes_all.html ,但我也想在该 href 中传递搜索查询。 I was trying to do {% url 'core:bikes_all' request.GET.cat=2 %} or {% url 'core:bikes_all' category_q=2 %} , but it didnt work.我试图做{% url 'core:bikes_all' request.GET.cat=2 %}{% url 'core:bikes_all' category_q=2 %} ,但没有奏效。

The search query looks like that, when I filter the results by category http://localhost:8000/bikes/all?cat=1 So my aim is to redirect user to http://localhost:8000/bikes/all?cat=2 , when he clicks on that a tag on first page.搜索查询看起来像这样,当我按类别过滤结果时http://localhost:8000/bikes/all?cat=1所以我的目标是将用户重定向到http://localhost:8000/bikes/all?cat=2 ,当他点击第一页上a tag时。

You are getting GET parameters in your request so you need to pass that GET parameter in your url like this:您正在请求中获取 GET 参数,因此您需要在您的 url 中传递该 GET 参数,如下所示:

https://url?parameter=2

so set the cat=i in your bikes_all url:所以在你的bikes_all网址中设置cat=i

{% url 'core:bikes_all' %}?cat=i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM