Am working on a name search app but having a challenge getting it to work with Django query string, the challenge which i observed comes from my JavaScript input type with ${input} string when added to Django query string eg. "address/get-name/?search=${input}" doesn't return the query object or the payload data, instead it return an empty list, but when i make search through the browser it return all search query data eg "address/get-name/?search=smith", Now how do i get input through my scripts without having to use the java scripts query input type ${input} with django so let i get a query string like this "address/get-name/?search=james" ? Here is my script, Am a novice in JavaScript
<script>
new Autocomplete('#autocomplete',{
search : input => {
console.log(input)
const url = "/address/get-names/?search=${input}"
return new Promise(resolve => {
fetch(url)
.then( (response) => response.json())
.then( data => {
console.log(data.playload)
resolve(data.playload)
})
})
},
renderResult : (result, props) =>{
console.log(props)
let group = ''
if (result.index % 3 == 0 ){
group = '<li >Group</li>'
}
return '${group}<li ${props}><div >${result.name}</div></li>'
}
})
</script>
my view.py
def get_names(request):
search = request.GET.get('search')
payload = []
if search:
objs = Names.objects.filter(name__startswith=search)
for obj in objs:
payload.append({
'name' : obj.name
})
return JsonResponse({
'200': True,
'playload': payload
})
CodePudding user response:
The problem is your javascript syntax, it's
const url = `/address/get-names/?search=${input}`
instead of
const url = "/address/get-names/?search=${input}"