Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Article Writing
  4. How to implement live search using fetch method on my test website ?

How to implement live search using fetch method on my test website ?

Scheduled Pinned Locked Moved Article Writing
helpjavascriptpythonregexquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Firaki12345
    wrote on last edited by
    #1

    Hi so i am using python Django and using JavaScript with the help of fetch method but i am stuck can anyone please help :( My python views File:

    def Data(request):
    items = Post.objects.all()

    data = \[\]
    
    for qs in items:
    
    	I = {"title":qs.title,
    		 "content": qs.content,
    		 "image": qs.image.url,
    		}
    	data.append(I)
    
    return JsonResponse({"data":data})
    

    My Search_results HTML FILE:

    {% extends 'blog/base.html' %}

    {% load static %}

    {% block content %}

    Search Results

    {% csrf\_token %}
    

    {% endblock content %}

    {% block js %}

    {% endblock js %}

    My JS FILE:

    console.log('Heelowwww')

    const url = window.location.href

    const searchForm = document.getElementById("search-form")
    const searchInput = document.getElementById("search-input")
    const resultsBox = document.getElementById("results-box")

    const csrf = document.getElementsByName("csrfmiddlewaretoken")[0].value

    options = {method: "GET",
    headers: {
    Accept: "application/json"
    },
    data:{
    'csrfmiddlewaretoken': csrf,
    // 'game':searchInput,
    }
    }

    const SearchPosts = async SearchIt => {
    const res = await fetch("http://localhost:8000/data/",options)
    const Posts = await res.json()

    S = Posts["data"]

    let matches = S.filter(post =>{
    const regex = new RegExp(`^${SearchIt}`, 'gi')
    return post.match(regex)
    })
    console.log(matches)

    }

    searchInput.addEventListener('input', () => SearchPosts(searchInput.value))

    I get Filter is not a fucntion/method error i am getting back an object so how can i do this :( sorry a beginner in javascript

    S 1 Reply Last reply
    0
    • F Firaki12345

      Hi so i am using python Django and using JavaScript with the help of fetch method but i am stuck can anyone please help :( My python views File:

      def Data(request):
      items = Post.objects.all()

      data = \[\]
      
      for qs in items:
      
      	I = {"title":qs.title,
      		 "content": qs.content,
      		 "image": qs.image.url,
      		}
      	data.append(I)
      
      return JsonResponse({"data":data})
      

      My Search_results HTML FILE:

      {% extends 'blog/base.html' %}

      {% load static %}

      {% block content %}

      Search Results

      {% csrf\_token %}
      

      {% endblock content %}

      {% block js %}

      {% endblock js %}

      My JS FILE:

      console.log('Heelowwww')

      const url = window.location.href

      const searchForm = document.getElementById("search-form")
      const searchInput = document.getElementById("search-input")
      const resultsBox = document.getElementById("results-box")

      const csrf = document.getElementsByName("csrfmiddlewaretoken")[0].value

      options = {method: "GET",
      headers: {
      Accept: "application/json"
      },
      data:{
      'csrfmiddlewaretoken': csrf,
      // 'game':searchInput,
      }
      }

      const SearchPosts = async SearchIt => {
      const res = await fetch("http://localhost:8000/data/",options)
      const Posts = await res.json()

      S = Posts["data"]

      let matches = S.filter(post =>{
      const regex = new RegExp(`^${SearchIt}`, 'gi')
      return post.match(regex)
      })
      console.log(matches)

      }

      searchInput.addEventListener('input', () => SearchPosts(searchInput.value))

      I get Filter is not a fucntion/method error i am getting back an object so how can i do this :( sorry a beginner in javascript

      S Offline
      S Offline
      Sean Ewington
      wrote on last edited by
      #2

      Thanks very much for using CodeProject. This is our forum for questions about posting articles. Please try posting this in our Quick Answers section: https://www.codeproject.com/Questions/ask.aspx[^]

      Thanks, Sean Ewington CodeProject

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups