1👍
✅
Create a url definition in your urls.py
which calls the API_search
function. In your UserDetailView
store this generated url inside an href
and when the user clicks on it, call your API search function.
OR
If you want to call the API_Search directly from the UserDetailView
call a HttpRedirectResponse to that url after generating the proper url.
EDIT:
Based on the extra information provided, you can use this:
def API_search(request):
user = request.user
token = UserTokens.objects.values_list('apiToken', flat=True).get(pk=user.id)
query = request.GET.get('query')
final_url = urllib2.Request('http://api.website.com', None, headers={'Content-Type':'application/json'})
#use the token as you like
base64string = base64.encodestring('%s:%s' % ('myusername', 'mypassword')).replace('\n', '')
final_url.add_header("Authorization", "Basic %s" % base64string)
json_obj = urllib2.urlopen(final_url)
readable_json = json.load(json_obj)
resultsOpen = []
for i in readable_json:
resultsOpen.append({
'subject': i['subject'],
})
return render(request, 'index/apiTest.html', {'objects_open': resultsOpen)
Source:stackexchange.com