작성
·
303
0
파이썬과 장고를 이제 막 공부를 시작해서 프로젝트를 만들고 있는데, 아직 장고 기초지식이 부족한지 해결을 하기 어렵습니다..
네이버 블로그 검색 api와 유튜브 api를 사용중이며,
구글처럼 메인페이지에서 검색을 하면, 그에 맞는네이버 블로그 글들과 유튜브 영상들이 나오게 하려고 합니다.
발생된 문제는,
검색 후 all.html 이라는 검색정보를 보여주는 페이지로 이동해야 되는데 왜인지 주소만 변경되고 이동이 되지않고 메인페이지로 유지됩니다. (http://127.0.0.1:8000/ -> http://127.0.0.1:8000/all )
<메인페이지>
<코드>
# urls.py
from django.conf import settings
from django.conf.urls import url, include
from django.conf.urls.static import static
from django.urls import path
from . import views
urlpatterns = [
url(r'^', views.index, name='index'),
path('all/', views.all, name="all"),
url(r'info/$', views.get_request_url, name="info"),
url(r'video/$', views.youtube, name="video"),
]
# views.py
import json, os, sys, urllib.request, requests
from django.shortcuts import render
from django.conf import settings
from django.views.generic import FormView
from requests import request
def index(request):
return render(request, 'common/main.html')
def make_naver_search_api_url(search_text, start_num, disp_num):
base_url = 'https://openapi.naver.com/v1/search/blog.json'
param_query = "?query=" + urllib.parse.quote(search_text)
param_start = "&start=" + str(start_num)
param_disp = "&display=" + str(disp_num)
return base_url + param_query + param_start + param_disp
def all(request):
# naver search value
search_text = request.POST.get('searchValue',"")
API_URL = make_naver_search_api_url(search_text,1,30)
config_secret_debug = json.loads(open(settings.SECRET_DEBUG_FILE).read())
client_id = config_secret_debug['NAVER']['CLIENT_ID']
client_secret = config_secret_debug['NAVER']['CLIENT_SECRET']
request = urllib.request.Request(API_URL)
request.add_header("X-Naver-Client-Id", client_id)
request.add_header("X-Naver-Client-Secret", client_secret)
response = urllib.request.urlopen(request)
rescode = response.getcode()
print(rescode)
if (rescode == 200):
# youtube search value
url = 'https://www.googleapis.com/youtube/v3/search'
params = {
'key': '유튜브api',
'part': 'snippet',
'type': 'video',
'maxResults': '30',
'q': search_text,
}
response = requests.get(url, params)
response_dict = response.json()
response_body = response.read()
naver_result = json.loads(response_body.decode('utf-8'))
naver_items = naver_result.get('items')
context = {
'items': naver_items,
'youtube_items': response_dict['items']
}
return render(request, 'searchView/all.html', {'info_items': context[0], 'video_items': context[1]})
else:
return None
print("---error---")
# main.html (메인페이지)
<body>
<div class="container">
<div class="mainBox">
<div class="logoBox">
<img src="{% static 'img/logo_transparent.png' %}" class="mainLogo">
</div>
<form class="d-flex" method="POST" action="{% url 'all' %}">
{% csrf_token %}
<div class="searchBox" style="display: flex">
<input class="form-control me-sm-2" type="text" placeholder="검색어를 입력" name="searchValue" value="{{ searchValue }}">
<button class="btn btn-info my-2 my-sm-0" type="submit">Search</button>
</div>
</form>
</div>
</div>
</body>
# all.html (페이지 이동 테스트 성공 후 작업예정) - 이 페이지는 무시하셔도 됩니다
{% extends 'common/base.html' %}
{% block content %}
<div class="contentFlexBox">
<div class="infoBox">
<div class="infoDetail">
{% for info in info_items %}
<a href="{{ info.link }}" class="list-group-item list-group-item-action flex-column align-items-start">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ info.title }}</h5>
<small class="text-muted">{{ info.bloggername }}</small>
</div>
<p class="mb-1">{{ info.description }}</p>
<small class="text-muted">{{ info.postdate | date:"Y-m-d"}}</small>
</a>
{% endfor %}
</div>
<div class="pageNavi">
<ul class="pagination">
<li class="page-item disabled">
<a class="page-link" href="#">«</a>
</li>
<li class="page-item active">
<a class="page-link" href="#">1</a>
</li>
<li class="page-item">
<a class="page-link" href="#">2</a>
</li>
<li class="page-item">
<a class="page-link" href="#">3</a>
</li>
<li class="page-item">
<a class="page-link" href="#">4</a>
</li>
<li class="page-item">
<a class="page-link" href="#">5</a>
</li>
<li class="page-item">
<a class="page-link" href="#">»</a>
</li>
</ul>
</div>
</div>
<div class="videoBox">
<a href="#" style="text-decoration: none;">
<div class="card mb-3">
<div class="card-body videoText">
<h5 class="card-title">영상제목</h5>
<h6 class="card-subtitle text-muted">채널주인</h6>
</div>
<svg xmlns="http://www.w3.org/2000/svg" class="d-block user-select-none" width="100%" height="200" aria-label="Placeholder: video cap" focusable="false" role="img" preserveAspectRatio="xMidYMid slice" viewBox="0 0 318 180" style="font-size:1.125rem;text-anchor:middle">
<rect width="100%" height="100%" fill="#868e96"></rect>
<text x="50%" y="50%" fill="#dee2e6" dy=".3em">video cap</text>
</svg>
<div class="card-body videoText">
<p class="card-text">동영상 소개글</p>
</div>
<div class="card-footer text-muted">
게시일
</div>
</div>
</a>
<div class="pageNavi">
<ul class="pagination">
<li class="page-item disabled">
<a class="page-link" href="#">«</a>
</li>
<li class="page-item active">
<a class="page-link" href="#">1</a>
</li>
<li class="page-item">
<a class="page-link" href="#">2</a>
</li>
<li class="page-item">
<a class="page-link" href="#">3</a>
</li>
<li class="page-item">
<a class="page-link" href="#">4</a>
</li>
<li class="page-item">
<a class="page-link" href="#">5</a>
</li>
<li class="page-item">
<a class="page-link" href="#">»</a>
</li>
</ul>
</div>
</div>
</div>
{% endblock %}
책도 보고 인터넷을 검색해도 도통 해결방법을 모르겠습니다..
혹시 어디부분이 문제인지 말씀해주시면 확인 후 수정하겠습니다..!
답변