IOIOIOIOIOIOI
IOIOIOIOIOIOI

Reputation: 287

Removing brackets and quotes from list in django template?

Context
I'm using Django to extract data from an API - which is then presented in a template. The data extracted is a list of genres. The list contains quotes and brackets when used via context.

Question
How to remove brackets and quotes from list in snippet below?

Snippet
enter image description here

Views.py

class HomePageView(TemplateView):
    # template_name = 'home.html'
    def get(self,request):
        if 'movie' in request.GET:
            api_key = xxxx
            id = request.GET['movie']
            url = 'https://api.themoviedb.org/3/search/movie?api_key={}&language=en-US&query={}&include_adult=false'
            # payload = {'q': , 'appid': 'xxxxxx'}
            response = requests.get(url.format(api_key,id))

            # successful request
            if response.status_code == 200:

                # Parse json output for key value pairs 
                tmdb = response.json()

                # backdrop image -- tmdb for each movie
                backdrop_path = tmdb['results'][0]['backdrop_path']
                url_backdrop = 'https://image.tmdb.org/t/p/original'+backdrop_path 

                # poster image -- tmdb for each movie
                poster_path = tmdb['results'][0]['poster_path']
                url_poster = 'https://image.tmdb.org/t/p/original'+poster_path     

                                # 2nd get request for info on individual movie
                id_1 = tmdb['results'][0]['id']
                api_key_1 = xxxx
                url = 'https://api.themoviedb.org/3/movie/{}?api_key={}&language=en-US'
                tmdb_1 = requests.get(url.format(id_1,api_key_1)).json()

                # genres list
                genres = []
                for i in range(len(tmdb_1['genres'])):
                    genres.append(tmdb_1['genres'][i]['name'])
                eeeee = str(genres)[1:-1]
                
                # prod comps list
                prodc = []
                for i in range(len(tmdb_1['production_companies'])):
                    prodc.append(tmdb_1['production_companies'][i]['name'])

                context = {
                    'title': tmdb['results'][0]['original_title'],
                    'overview': tmdb['results'][0]['overview'],
                    'release_date': tmdb['results'][0]['release_date'],
                    'vote_average': tmdb['results'][0]['vote_average'],
                    'vote_count': tmdb['results'][0]['vote_count'],
                    'backdrop_path' : tmdb['results'][0]['backdrop_path'],
                    'backdrop' : url_backdrop,
                    'poster' : url_poster,
                    'runtime' : tmdb_1['runtime'],
                    'boxoffice' : tmdb_1['revenue'],
                    'tagline' : tmdb_1['tagline'],
                    'genres' : genres,
                    'prodc' : prodc
                    }
                return render(request, 'home.html', {'context': context}) 
            

            else: # returns homepage if invalid name is given in form
                    return render(request, 'home.html')

        else: # Homepage without GET request
            return render(request, 'home.html')
            

HTML

    {% if context.title %}
    <div class="w-7/12 col-span-1 pcard">   
        <div  class="wh pb-4 lato">{{context.title}}</div>
        <div class="gr tagline">{{context.tagline}}</div>
        <div class="wh py-3">{{context.overview}}</div>
        <div class="gr pt-3">{{context.genres}}</div>
        <div class="wh pb-3">{{context.prodc}}</div>
    {% endif %}

Upvotes: 0

Views: 1486

Answers (1)

IOIOIOIOIOIOI
IOIOIOIOIOIOI

Reputation: 287

Solution
The solutions suggested by @nigel222 in the comment above works best. Refer to the link on how the Django template operator pipe works.

HTML

 {% if context.title %}
    <div class="w-7/12 col-span-1 pcard">   
        <div  class="wh pb-4 lato">{{context.title}}</div>
        <div class="gr tagline">{{context.tagline}}</div>
        <div class="wh py-3">{{context.overview}}</div>
        <div class="gr pt-3">{{ context.genres|join:", " }}</div>
        <div class="wh pb-3">{{context.prodc|join:", "}}</div>
    {% endif %}

Snippet
enter image description here

Upvotes: 1

Related Questions