Django API: Ping-Pong ๋ง๋ค๊ธฐ
ํด๋น ํ๋ก์ ํธ์์ pip list ์ฐพ๊ฒ ํ๊ธฐ
๊ฐ์ ํ๊ฒฝ ์์ pip ์ค์น
(venv)
chloe@chloe-XPS-15-9570 ~/Workspace/VanillaJS/live/AJAX
$ pip -m pip install --upgrade pip
๊ฐ์ ํ๊ฒฝ ์์ django ์ค์น
(venv)
chloe@chloe-XPS-15-9570 ~/Workspace/VanillaJS/live/AJAX
$ pip install django==2.1.15
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>์-ํธ</title>
</head>
<body>
<label for="userInput">Input: </label>
<input id="userInput" name="inputText" type="text">
<pre id="resultArea">
</pre>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
// 1. input#userInput ์ 'change' ์ด๋ฒคํธ ๋, value๊ฐ์ '/art/pong' ์ผ๋ก ๋ณด๋ธ๋ค
const userInput = document.querySelector('#userInput')
userInput.addEventListener('input', function(event){
// input type="text"๋ focus out === enter
const inputText = userInput.value
// 2. pong์ด ๋ฐ์์ ๋ค์ JSON์ผ๋ก ์๋ต์ ๋ณด๋ธ๋ค
axios.get('/arts/pong/', {
params: {
inputText: inputText,
}
})
.then(function(res){
// 3. ์๋ต JSON์ ๋ด์ฉ์ div#resultArea์ ํ์ํ๋ค
const resultArea = document.querySelector('#resultArea')
resultArea.innerText = res.data.content
})
})
</script>
</body>
</html>
from django.shortcuts import render
from django.http import JsonResponse
import art
# Create your views here.
def ping(request):
return render(request, 'arts/ping.html')
def pong(request):
user_input = request.GET.get('inputText')
art_text = art.text2art(user_input)
data = {
'success': True,
'content': art_text,
}
return JsonResponse(data)
์ข์์ AJAX๋ก ๊ตฌํํ๊ธฐ
<div data-gb-custom-block data-tag="extends" data-0='base.html'></div>
<div data-gb-custom-block data-tag="block">
<h2>INDEX</h2>
<div data-gb-custom-block data-tag="for">
<h3>์์ฑ์: {{ article.user }}</h3>
<h4>์ ๋ชฉ: {{ article.title }}</h4>
<p>๋ด์ฉ: {{ article.content }}</p>
<span>AJAX ๋ฐฉ์</span>
<div data-gb-custom-block data-tag="if">
<i class="fas fa-heart fa-lg likeButtons" style="color:crimson" data-id="{{article.pk}}"></i>
<div data-gb-custom-block data-tag="else"></div>
<i class="fas fa-heart fa-lg likeButtons" style="color:black" data-id="{{article.pk}}"></i>
</div>
<span>๊ธฐ์กด ๋ฐฉ์</span>
<a href="
<div data-gb-custom-block data-tag="url" data-0='articles:like'></div>">
<div data-gb-custom-block data-tag="if">
<i class="fas fa-heart fa-lg" style="color:crimson"></i>
<div data-gb-custom-block data-tag="else"></div>
<i class="fas fa-heart fa-lg" style="color:black"></i>
</div>
</a>
<span id="likeCount-{{article.pk}}">{{article.like_users.all|length}}</span> ๋ช
์ด ์ด ๊ธ์ ์ข์ํฉ๋๋ค.
<hr>
</div>
<a href="
<div data-gb-custom-block data-tag="url" data-0='articles:create'></div>">CREATE</a>
<script>
const likeButtonList = document.querySelectorAll('.likeButtons')
likeButtonList.forEach(likeButton => {
likeButton.addEventListener('click', e => {
// 1. axios๋ก ์์ฒญ๋ณด๋ด๊ธฐ(like)
//const articleID = e.target.getAttribute('data-id')
const articleID = e.target.dataset.id
// -> data- ๋ก ์์ํ๋ attribute๋ dataset์ ์ ์ฅ๋๊ณ , dash ๋ค์ id๋ก ๋ฐ๋ ค์ฌ ์ ์์
<div data-gb-custom-block data-tag="if">
axios.get(`/articles/${articleID}/like_api/`)
.then( res => {
// ๊ฒฐ๊ณผ ๋ฐ์ ๋ค์ ํ ๊ฒ๋ค
likeCount = document.querySelector(`#likeCount-${articleID}`).innerText = res.data.count
// ํ์ฌ db์ ์ ์ฅ๋ ๊ฐ์ด liked=True ๋ผ๋ฉด,
if (res.data.liked){
e.target.style.color = 'crimson'
}else{
e.target.style.color = 'black'
}
})
<div data-gb-custom-block data-tag="else"></div>
alert('๋น ๋ก๊ทธ์ธ ์ฌ์ฉ์๋ ์ข์์๋ฃฐ ๋๋ฅผ ์ ์์ด์ ใ
_ใ
')
</div>
})
})
</script>
</div>
@login_required
def like_api(request, article_pk):
user = request.user
article = get_object_or_404(Article, pk=article_pk)
if article.like_users.filter(pk=user.pk).exists():
article.like_users.remove(user)
liked = False
else:
article.like_users.add(user)
liked = True
context = {
'liked': liked,
'count': article.like_users.count()
}
return JsonResponse(context)