-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_product.html
65 lines (50 loc) · 1.63 KB
/
create_product.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{% extends 'base.html' %}
{% block title %}
책 등록
{% endblock title %}
{% block content %}
<h1>책 등록</h1>
<form method="post" class="productCreate">
{% csrf_token %}
<label for="name">책 제목:</label>
<input type="text" id="name" name="name" required /><br /><br />
<label for="price">가격:</label>
<input type="text" id="price" name="price" required /><br /><br />
<label for="image">책 이미지:</label>
<input type="file" id="image" name="image" accept="image/*" required /><br /><br />
<label for="user">등록자:</label>
<input type="text" id="user" name="user" value="{{ user.nickname }}" readonly /><br /><br />
<input type="submit" value="등록" />
</form>
<script>
document.querySelector(".productCreate").addEventListener("submit", function (e) {
e.preventDefault();
const fileInput = document.querySelector("#image");
const imageFile = fileInput.files[0];
const reader = new FileReader();
reader.onloadend = function () {
const imageData = reader.result.split(",")[1];
const data = {
name: document.querySelector("#name").value,
price: document.querySelector("#price").value,
image: imageData
};
const csrfToken = getCookie("csrftoken");
fetch("/products/book/create/", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRFToken": csrfToken
},
body: JSON.stringify(data)
})
.then(res => res.json())
.then(data => {
alert(data.message);
window.location.href = data.redirect_url;
});
};
reader.readAsDataURL(imageFile);
});
</script>
{% endblock content %}