Created
April 20, 2020 09:29
-
-
Save RyanKor/b20de3cb0704be4ff871db6b271a15b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def home(request): | |
# posts = Post.objects.all() | |
url_parameter = request.GET.get("q", None) # request에서 query 값을 인식한다는 뜻이다. | |
# 중요한 건 여깁니다. “q”는 query의 약자로 사용자가 질문한 값을 여기서 get하겠다는 의미 | |
if url_parameter: # 사용자가 입력한 query 값이 있다면, | |
results = Post.objects.filter(title__contains=url_parameter) | |
else: | |
results = Post.objects.all() # 없으면 저장된 객체값을 모두 보여달라는 뜻 | |
if request.is_ajax(): # 요청 자체가 AJAX라면, | |
html = render_to_string( # 결과값을 부분만 보여주는 HTML 렌더링 | |
template_name="search_result.html", context={"results": results}) | |
data_dict = {"html_from_view": html} | |
return JsonResponse(data=data_dict, safe=False) | |
return render(request, 'home.html', {'results': results}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment