Django如何使用多个关键字进行过滤

时间:2013-04-18 09:34:15

标签: django

我正在根据“keyskill”过滤数据库中的数据。例如,我有以下技能

  1. Java
  2. django的
  3. HTML
  4. 的CSS
  5. 在我的搜索框中使用关键字“python”进行过滤时,将从db中获取与该关键字相关的数据。但是当我在搜索框中搜索关键字“python,Java”时,没有提取任何细节。

    如何使用两个关键字从数据库中过滤数据。

    views.py

    def search_result(request):
    
        search = request.GET['skills']
        query = Q(key_skills__icontains=search )
        details = jobs.objects.filter(query)
        return render_to_response('registration/searchresult.html', {'details': details})
    

    models.py

    class jobs(models.Model):
        title = models.CharField(max_length=30)
        jobsummary = models.TextField()
        jobdetails = models.TextField()
        key_skills = models.CharField(max_length=30)
        min_exp = models.IntegerField(default=0)
        max_exp = models.IntegerField(default=0)
    
      

    示例在我的作业表中有5个具有关键技能的记录(1.java 2.Python 3.Html ..等...)在搜索框中输入“Java”时,它显示了他们的详细信息,但在搜索框中输入作为“java Html”它没有显示任何东西,我需要在这里显示java和html细节

1 个答案:

答案 0 :(得分:0)

file.html

$(document).keypress(function(event){
    var keycode = (event.keyCode ? event.keyCode : event.which);
    if(keycode == '13'){
        var keyword = $("#id_search_field").val();
        if ( keyword == 'SEARCH') {
            keyword = '';
        }
        if ((keyword != '')){
            window.location.href = "/search_view/?key=" + keyword
        }
        window.location.href = "/search_view/?keyword=" + keyword



        }
    });

    function keyword(){

        var keyword = $("#id_search_field").val();
        if ( keyword == '{% trans "SEARCH" %}') {
            keyword = '';
        }
        if (keyword != ''){
            window.location.href = "/search_view/?key=" + keyword
        }
        window.location.href = "/search_view/?keyword=" + keyword

    }

    <body>

    <input type="text" name="place" placeholder="SEARCH" id="id_search_field"/></div>
    <div class="searc">
      <a href="#"><img src="/static/img/admin/search.png" alt="searchbox" onClick="keyword();"/></a>
    </div>
    {% for i in list %}
      {{i.keyskill}}
    {% endfor %}   

    </body>

search_view(Your views.py)

def search_view(request): 
    key = request.GET.get('keyword')
    if key is not None: 
        key = key.lstrip()
    if key :
        list = Dbtablename.objects.filter(Q(keyskill__icontains = key)).order_by('-modified_at')
    else:
        list = Dbtablename.objects.filter().order_by('-modified_at')