def search(request): context = {} keyword = request.GET.get('keyword') page_token = request.GET.get('page_token') if keyword: search_response = youtube_search(keyword, page_token) video_id_list = [] for item in search_response['items']: if item['id'].get('videoId'): video_id_list.append(item['id']['videoId']) video_response = youtube_video(video_id_list, search_response) video_id_list = [item['id'] for item in video_response['items']] exist_list = Video.objects.filter( Q(youtube_id__in=video_id_list) & Q(users__pk=request.user.pk)) exist_id_list = [video.youtube_id for video in exist_list] for item in video_response['items']: cur_video_id = item['id'] if cur_video_id in exist_id_list: item['is_exist'] = True response = video_response context['keyword'] = keyword context['response'] = response return render(request, 'video/search.html', context)
def search(request): # Template : 'video/search.html' # URL : 'video/search/' # view : search # Static, Templates 디렉토리 설정 """ 1. STATICFILES_DIRS 설정 2. templates폴더 생성 후 TEMPLATE의 DIRS안에 설정 3. video/search.html파일 생성 4. search.html내부 내용 input ul > li (img, p*3) 5. urls.py에 view연결 6. view에서 video/search.html파일 render """ # GET paramter에서 keyword값을 가져옵니다 # 빈 dict요소 생성 context = {} # request.GET의 parameter에서 keyword, page_token값을 가져옴 keyword = request.GET.get('keyword') page_token = request.GET.get('page_token') # 만약 keyword값이 존재할 경우 (request.GET의 keyword키로 값이 넘어올 경우) # context dict에 keyword, response에 값 할당해줌 if keyword: response = youtube_search(keyword, page_token) context['keyword'] = keyword context['response'] = response # 검색을 안 할 경우 빈 dict요소만 가지고 렌더링 return render(request, 'video/search.html', context)
def search(request): context = {} keyword = request.GET.get('keyword') page_token = request.GET.get('page_token') if keyword: response = youtube_search(keyword, page_token) context['keyword'] = keyword context['response'] = response return render(request, 'video/search.html', context)
def search(request): keyword = request.GET.get( 'q', None ) page_token = request.GET.get( 'page_token', None ) if not keyword: return render( request, 'video/search.html', {} ) # if keyword: result = youtube_search( keyword, page_token ) # print(json.dumps(result, indent=2, sort_keys=True)) context = { 'keyword': keyword, 'result': result, } return render( request, 'video/search.html', context )