【问题标题】:Search from every pages in django从 django 中的每个页面搜索
【发布时间】:2020-02-21 07:36:41
【问题描述】:

我想在我的网站中添加搜索,我想从我的页面中搜索 (html)

** 注意:我想获取标签标签,例如:

我有一个名为“test”的标签,当用户在搜索栏中写“test”时,我想在新页面中查看用户的标签

  • 我的标签是这样的:
<label id="label_main_app">
    <img id="img_main_app_first_screen" src="{% static " images/android_studio.jpg " %}" alt=""> test
    <br>
    <br>
    <p id="p_size_first_page">this is an test app
        <br>
        <br>
        <a href="https://www.fb.com" type="button" class="btn btn-primary"><big> See More & Download </big></a>
    </p>
</label>
  • 我总是在结果页面中收到此消息:未找到

我的代码:* html 主页中的这个表单

<form class="" action="{% url 'test_search_page'%}" method="get">
    <div class="md-form mt-0 search-bar" id="search_form">
        <input id="search_box" autocomplete="off" onkeyup="searchFunction()" class="form-control" type="text" placeholder="Write Here to Search ..." aria-label="Search" name="search">
    </div>
</form>

这是我的 view.py 代码:

def search(request):
    input= 'search'
    my_template_keyword = ['label']
    if 'search' in request.GET and request.GET['search'] and input == my_template_keyword:
        return render(request, 'home_page/testforsearch.html', {'search:':search})` 

最后这是我在 html 结果页面中的代码:

<div class="container">
    {% if search %}
        {{ search }}
    {% else %}
        <h2>not found</h2>
    {% endif %}
</div>

有什么帮助吗?

【问题讨论】:

  • 我不明白的是在views.py 中,您首先分配input='search',然后分配my_template_keyword = ['label']。在您的if 语句中,测试input == my_template_keyword 是否绝对不相等?
  • 是的,它不相等,我必须做什么?

标签: python django python-3.x django-templates


【解决方案1】:

我看不到数据库搜索的查询。使用这样的通用显示视图:

from django.views.generic.list import ListView
from .models import Videos


class Search(ListView):
    model = Videos
    template_name = "/search.html"
    paginate_by = 30

    def get_queryset(self):
        query = self.request.GET.get('search')
        if query:
            object_list = self.model.objects.filter(title__icontains=query)
        else:
            object_list = self.model.objects.none()
        return object_list

【讨论】:

  • 我已经尝试过了,但对我没有任何其他帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-02
  • 2011-02-26
  • 2017-12-22
  • 2014-01-10
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多