I’m trying to make a searchbar in my django app. When I click on a result from the searchbar I want it to call my view. I want the view to keep the context from what was clicked, perform a function, and then redirect me.
In my views.py
def search(request):
query = request.GET.get('q', '')
if query:
sp = SpotipyConnection(client_id=sp_client_id, client_secret=sp_client_secret)
#context = {'search': sp.searchbar(query)}
context = {'search': sp.songSearch(query)}
return render(request,'main/search.html',context=context)
return render(request,'main/search.html',{})
def search_results(request):
if request.method == 'POST':
#TODO how do i get the context of what was clicked
songName = None
songArtist = None
context = openAISearch(artist=songArtist,song=songName)
return render(request,'main/search_results.html',context=context)
In my search.html:
{% block title %}
Search
{% endblock %}
{% block content %}
<script>
function redirectToHome(songName, songArtist) {
<!-- TODO how do I call my search_results function then return the redirect -->
}
</script>
<form method="GET" action="{% url 'search' %}">
<input type="text" name="q" id="search-input">
<button type="submit">Search</button>
</form>
</form>
{% if search %}
{% for result in search %}
<p onclick="redirectToHome({{ result.name }},{{ result.artists.0.name }})"> {{result.name}} by {{ result.artists.0.name }} <img src="{{ result.album.images.0.url }}" alt="{{result.name}} image" style="width: 200px; height: 200px;"> </p>
{% endfor %}
{% endif %}
{% endblock %}```