Django模板中的搜索字段

Django模板中的搜索字段,django,search,django-templates,field,Django,Search,Django Templates,Field,Django模板中的搜索字段 如何在Django模板中创建类似于此图像的搜索字段 我在github尝试过这个 但我不知道如何完成。您所说的搜索字段实际上只是一个表单,您有一个输入(搜索框),并且您在视图中收到该输入 管理表单和获取操作的小示例: 视图。py: def your_view(request): ''' This could be your actual view or a new one ''' # Your code if request.method

Django模板中的搜索字段

如何在Django模板中创建类似于此图像的搜索字段

我在github尝试过这个


但我不知道如何完成。

您所说的搜索字段实际上只是一个表单,您有一个输入(搜索框),并且您在视图中收到该输入

管理表单和获取操作的小示例:

视图。py:

def your_view(request):
    ''' This could be your actual view or a new one '''
    # Your code
    if request.method == 'GET': # If the form is submitted

        search_query = request.GET.get('search_box', None)
        # Do whatever you need with the word the user looked for

    # Your code
模板

在模板中,最重要的是表单,您应该有如下内容:

# Your template code
<form type="get" action="." style="margin: 0">
    <input  id="search_box" type="text" name="search_box"  placeholder="Search..." >
    <button id="search_submit" type="submit" >Submit</button>
</form>
# Your template code

无论如何,我建议您检查一些信息,如:

前端:模板

使用搜索框创建表单(使用html、css)

  • 您的url应与**表单(模板)**中的相同,并且您的_视图应与views.py中的相同(视图


  • 你现在看到了什么?您正在调用哪个url/视图?你已经试过什么了?我决定了这是行不通的
    url(r'^your_url/?$', 'yourproject.views.your_view', name='your_url_name'),
    
    <form type="get" action="exact_url" > 
        <input  id="search_box" type="text" name="search_box"  placeholder="Search..." > 
        <button id="search_submit" type="submit" >Submit</button> 
    </form> 
    
       def your_view(request):
          if request.method == GET:  
              search_text = request.GET.get(search_box", None)
              records=Table.objects.filter(columnn__contains=search_text)     
              from django.http import JsonResponse
              return JsonResponse({"result_records":records})
    
       url(r'^exact_url/?$', 'yourproject.views.your_view')