onclick = window.location.href = "/"
{{ 값 }}
return render_template("index.html", name=name, rows=rows)
<li>{{ rows[0].MSRSTE_NM }}: {{ rows[0].IDEX_MVL }}</li>
{% set gu_name = rows[0].MSRSTE_NM %}
{% set gu_mise = rows[0].IDEX_MVL %}
<li>{{ gu_name }}: {{ gu_mise }}</li>
{% for row in rows %}
{% set gu_name = row.MSRSTE_NM %}
{% set gu_mise = row.IDEX_MVL %}
<li>{{ gu_name }}: {{ gu_mise }}</li>
{% endfor %}
{% if gu_mise >= 50 %}
<li>{{ gu_name }}: {{ gu_mise }}</li>
{% endif %}
@app.route('/detail/<keyword>')
def detail(keyword):
return render_template("detail.html", word=keyword)
https://owlbot.info/
r = requests.get("https://owlbot.info/api/v4/dictionary/owl", headers={"Authorization": "Token [내토큰]"})
result = r.json()
print(result)
$.ajax({
type: "GET",
url: "https://owlbot.info/api/v4/dictionary/owl",
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Token [내토큰]");
},
data: {},b
error: function (xhr, status, error) {
alert("에러 발생!");
},
success: function (response) {
console.log(response)
}
})
https://fontawesome.com/v4.7.0/
let word = '{{ word }}'
$(document).ready(function () {
get_definitions()
})
function get_definitions() {
$.ajax({
type: "GET",
url: `https://owlbot.info/api/v4/dictionary/${word}`,
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Token [내토큰]");
},
data: {},
error: function (xhr, status, error) {
alert("에러 발생!");
},
success: function (response) {
console.log(response)
}
})
}
2) API에서 받은 값들을 해당하는 태그에 넣어준다.
$("#word").text(response["word"])
$("#pronunciation").text(`/${response["pronunciation"]}/`)
$("#definitions").empty()
let definitions = response["definitions"]
for (let i=0;i<definitions.length;i++) {
let definition = definitions[i]
let html_temp = `<div style="padding:10px">
<i>${definition["type"]}</i>
<br>${definition["definition"]}<br>
<span class="example">${definition["example"]}</span>
</div>`
$("#definitions").append(html_temp)
}
3) 발음이 null일 경우 빈 텍스트를 넣어준다.
if (response["pronunciation"]==null) {
$("#pronunciation").text("")
} else {
$("#pronunciation").text(`/${response["pronunciation"]}/`)
}
4) 예문이 null일 경우 예외처리를 해준다.
let html_temp = ``
if (definition["example"]!=null) {
html_temp = `<div style="padding:10px">
<i>${definition["type"]}</i>
<br>${definition["definition"]}<br>
<span class="example">${definition["example"]}</span>
</div>`
} else {
html_temp = `<div style="padding:10px">
<i>${definition["type"]}</i>
<br>${definition["definition"]}<br>
</div>`
}
$("#definitions").append(html_temp)
r = requests.get(f"https://owlbot.info/api/v4/dictionary/{keyword}", headers={"Authorization": "Token [내토큰]"})
result = r.json()
print(result)
2) html에서 결과가 들어가야할 부분을 표시해준다.
3) 발음이 있는 경우에만 보여주도록 예외처리를 해준다.
{% if result.pronunciation %}
<h5 id="pronunciation" style="display: inline;">/{{ result.pronunciation }}/</h5>
{% endif %}
4) 예문이 있는 경우에만 보여주도록 예외처리를 해준다.
{% if definition.example %}
<span class="example">{{ definition.example }}</span>
{% endif %}
5) 예문에 HTML 태그 쓰는 것을 허용해준다.
<span class="example">{{ definition.example|safe }}</span>
6) 정의와 예문에서 깨진 글자를 없앤다.
<br>{{ definition.definition.encode('ascii', 'ignore').decode('utf-8') }}<br>
{% if definition.example %}
<span class="example">{{ definition.example.encode('ascii', 'ignore').decode('utf-8') }}</span>
{% endif %}
status_give
라는 이름으로 온 파라미터를 다시 status
라는 이름으로 템플릿에 보내준다.status_receive = request.args.get("status_give")
return render_template("detail.html", word=keyword, result=result, status=status_receive)
status_give
라는 파라미터를 주지 않을 경우를 대비해, 기본값을 "new"
로 준다.status_receive = request.args.get("status_give", "new")
{% if status=="new" %}
<button id="btn-save" class="btn btn-outline-sparta btn-lg" onclick="save_word()">
<i class="fa fa-floppy-o"></i>
</button>
{% else %}
<button id="btn-delete" class="btn btn-sparta btn-lg" onclick="delete_word()">
<i class="fa fa-trash-o"></i>
</button>
{% endif %}
words
컬렉션에 저장한다.@app.route('/api/save_word', methods=['POST'])
def save_word():
# 단어 저장하기
word_receive = request.form['word_give']
definition_receive = request.form['definition_give']
doc = {"word": word_receive, "definition": definition_receive}
db.words.insert_one(doc)
return jsonify({'result': 'success', 'msg': f'word "{word_receive}" saved'})
2) 클라이언트에서는 단어와 첫 번째 정의만 POST 요청으로 보내준다. 단어 저장에 성공하면 얼럿을 띄운 후, status=old
로 바뀐 페이지를 띄워준다. 저장 버튼에 onclick=save_word()
로 연결해준다.
[save_word()시작코드)
function save_word() {
$.ajax({
type: "POST",
url: `/api/save_word`,
data: {},
success: function (response) {
alert(response["msg"])
}
});
}
function save_word() {
$.ajax({
type: "POST",
url: `/api/save_word`,
data: {
word_give: "{{ word }}",
definition_give: "{{ result.definitions[0].definition }}"
},
success: function (response) {
alert(response["msg"])
window.location.href = "/detail/{{ word }}?status_give=old"
}
});
}
@app.route('/api/delete_word', methods=['POST'])
def delete_word():
# 단어 삭제하기
word_receive = request.form['word_give']
db.words.delete_one({"word":word_receive})
return jsonify({'result': 'success', 'msg': f'word "{word_receive}" deleted'})
2) 클라이언트에서는 단어를 보내주고, 단어 삭제에 성공하면 더이상 보여줄 정보가 없으므로 얼럿을 띄운 후 메인 페이지로 이동한다.
[delete_word()] 시작 코드
function delete_word() {
$.ajax({
type: "POST",
url: `/api/delete_word`,
data: {},
success: function (response) {
alert(response["msg"])
}
});
}
function delete_word() {
$.ajax({
type: "POST",
url: `/api/delete_word`,
data: {
word_give: '{{ word }}',
},
success: function (response) {
alert(response["msg"])
window.location.href = "/"
}
});
}
@app.route('/')
def main():
# DB에서 저장된 단어 찾아서 HTML에 나타내기
words = list(db.words.find({}, {"_id": False}))
return render_template("index.html", words=words)
3) index.html에서는 각 단어마다 테이블의 한 줄이 되도록 넣어준다.
<tbody id="tbody-box">
{% for word in words %}
<tr id="word-{{ word.word }}">
<td><a href="/detail/{{ word.word }}?status_give=old">{{ word.word }}</a></td>
<td>{{ word.definition|safe }}
</td>
</tr>
{% endfor %}
</tbody>
let words = {{ words|tojson }};
let word_list = [];
for (let i = 0; i < words.length; i++) {
word_list.push(words[i]["word"])
}
2) 단어를 검색했을 때 단어 리스트에 있는 경우에는 행을 하이라이트하고, 없는 단어 일 때는 단어 상세페이지로 넘어가는 기능을 만든다.
function find_word() {
let word = $("#input-word").val().toLowerCase();
if (word == "") {
// 빈 문자열이면 얼럿
alert("please write something first :)")
return
}
if (word_list.includes(word)) {
// 리스트에 있으면 하이라이트
$(`#word-${word}`).addClass('highlight').siblings().removeClass('highlight');
$(`#word-${word}`)[0].scrollIntoView();
} else {
// 리스트에 없으면 상세 페이지로
window.location.href = `/detail/${word}?status_give=new`
}
}
main
으로 리다이렉팅 시킨다.if r.status_code != 200:
return redirect(url_for("main"))
3) 단어 찾기 실패 얼럿을 띄우려면 redirect()
에 메시지를 같이 전달한다.
url_for("main", msg="Word not found in dictionary; Try another word")
4) main()
에서 메시지를 받아 템플릿에 같이 보내준다.
@app.route('/')
def main():
# DB에서 저장된 단어 찾아서 HTML에 나타내기
msg = request.args.get("msg")
return render_template("index.html", words=words, msg=msg)
5) index.html에서 msg
가 있을 때 해당 메시지로 얼럿을 띄운다.
{% if msg %}
alert("{{ msg }}")
{% endif %}