0

I am trying to implement a searchbar in django. I am getting the search input as movie_title from the html form. Now, how do i include it in my api call ? I tried with curly braces. Here's is the code

def searchbar(request):
    if request.method == 'GET':
        movie_title = request.GET.get('movie_title')
        searched_movie = requests.get(
            'http://www.omdbapi.com/?apikey=9a63b7fd&t={movie_title}')
3
  • use f strings f"" Commented Feb 14, 2021 at 11:19
  • 1
    just concat the string with the variable Commented Feb 14, 2021 at 11:25
  • thanks , my python is a little rusty. figured it out. Commented Feb 15, 2021 at 5:44

2 Answers 2

2

You can create the url as an object using f-strings (one of the ways) and pass that to the get() method:

url = f'http://www.omdbapi.com/?apikey=9a63b7fd&t={movie_title}'
searched_movie = requests.get(url)

Note: You don't need to create a different object and can directly use:

searched_movie = requests.get(f'http://www.omdbapi.com/?apikey=9a63b7fd&t={movie_title}')

The above approach helps with readability when there are several dynamic attributes involved.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use { and } in your query string you can simply write

searched_movie = requests.get('http://www.omdbapi.com/?apikey=9a63b7fd&t={'+movie_title+'}')  

Otherwise you can write

searched_movie = requests.get('http://www.omdbapi.com/?apikey=9a63b7fd&t='+movie_title) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.