2025年5月30日 星期五

尚虎雲產銷平台助雲林發展農(漁)藝產業

國立虎尾科技大學永續發展暨社會責任處

113學年度第二學期社會責任實踐教育(四)-產銷平台與智慧生活課程教學成果



教學目標:

  • 課程大綱: 本課程以問題導向學習法,結合生成式AI、Python程式設計,帶領學生解決農村農業生產監控與行銷相關問題,並利用Django開發農產品產銷平台。透過數據分析掌握農作物產銷狀況,學生將學習如何應用政府開放數據來分析台灣各地的風土經濟,並以5W2H工具發掘在地問題。解方設計部分,將教授服務設計與食物設計理念,幫助學生創造解決方案。實作階段則涵蓋如何運用Python進行產銷資訊服務的開發。最終,學生將學會設計永續經營的商業模式,培養社會責任實踐精神與創業家思維。
  • SDG 11 永續城鄉的課程目標:本課程針對SDG 11的課程目標是培養學生在農村和農業領域的永續發展能力,透過數據分析和AI技術協助學生了解不同鄉鎮的風土經濟問題,並設計智慧農業解決方案。學生將學習如何運用科技改善農村的生產效率,提升農產品產銷平台的運作,促進鄉村經濟活化,並同時保護自然環境,達成城鄉永續發展的目標。
  • SDG 12 責任消費與生產的課程目標:針對SDG 12的課程目標是教導學生負責任的消費與生產模式,特別是在農業與食品生產鏈中的應用。學生將學習如何利用Python和生成式AI進行數據分析,優化農產品的生產與供應鏈管理,並設計符合環保和永續經營的商業模式。課程還將引導學生考量資源使用與廢棄物管理,提倡低碳、環保的消費與生產習慣,促進資源的有效運用與循環經濟。

場域學習:
  • 114年3月21日生機廚房-慢食。花生。設計所





  • 114年5月10日-成龍濕地和箔子竂漁港




業師教學:
  • 114年4月18日由聯合國永續發展談飲食與健康/南開科技大學餐飲管理系紀璟叡助理教授


  • 114年5月9日使用樹莓派+MCU構建智慧養殖學習平台/致用高中賴銀泉教師


  • 114年5月16日蜜之園有機農場創業分享/蜜之園有機農場場長蔡進煌


  • 114年5月23日Good家博士的淨零挑戰以及來自偉大航道的海灘料理/國立自然科學博物館廖仁滄服務員


成果影片:

開源程式碼:

基礎教學:

進階教學

專題實作:
應用範例:

評量工具:
商業模式圖:





尚虎雲產銷平台-銷售端整合商業模式圖

 


參考文章:尚虎雲產銷平台-銷售端整合市場行情
圖示下載網站:flaticon

1. Sales-side/apps/templates/home/Front page.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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
{% extends "layouts/base.html" %}
{% load static %}
{% comment %} 這個是網頁的title {% endcomment %}
{% block title %}
  {% if web_inf.title %}
    {{ web_inf.title }}
  {% else %}
    尚虎雲
  {% endif %}
{% endblock title %}

{% comment %} 在這裡加入css {% endcomment %}
{% block stylesheets %}
<link href="{{ ASSETS_ROOT }}/css/Front page.css" rel="stylesheet" media="all" autostart="true">
<style>
  .active { /* 示例樣式 */
      color: red;
  }
  .slide {
      display: inline-block;
      width: 100%;
  }
  .carousel-container {
      width: 100%;
      overflow: hidden;
  }
  .carousel-inner {
      display: flex;
      transition: margin-left 0.5s ease;
  }
  table, th, td {
  border: 1px solid white;
  border-collapse: collapse;
}
</style>
{% endblock stylesheets %}

{% block container %}
<!--首頁-->
<div class="bg1">
  <div class="nav">
    <a href="#" class="logo">
      <img src="{% if web_inf.logo %}{{ web_inf.logo.url }}{% else %}{{ ASSETS_ROOT }}/images/favicon.png{% endif %}">
    </a>
    <div class="nav-text">
      <a href="#home">主頁</a>
      <a href="#trading">交易行情</a>
      <a href="#about">關於我們</a>
      <a href="#menu">賣家</a>
      <a href="#business">商業模式圖</a>
      <a href="#contact">聯絡我們</a>
    </div>
  </div>
</div>
<!--首頁-->

<!--圖片輪播-->
<section class="home" id="home">
  <div class="slider">
    <!-- list Items -->
    <div class="list">
      {% for carousel in carousels %}
        {% if forloop.first %}
          <div class="item active">
        {% else %}
          <div class="item">
        {% endif %}
        <img src="{{ carousel.image.url }}">
            <div class="content" style="color: {{ carousel.color }}">
              {% if carousel.header %}
                <p>
                  {{ carousel.header }}
                </p>
              {% endif %}
              {% if carousel.title %}
                <h2>
                  {{ carousel.title }}
                </h2>
              {% endif %}

              {% if carousel.content %}
                <p>
                  {{ carousel.content }}
                </p>
              {% endif %}
            </div>
          </div>
      {% endfor %}
    </div>

    <!-- button arrows -->
    <div class="arrows">
        <button id="prev"><</button>
        <button id="next">></button>
    </div>

    <!-- thumbnail -->
    <div class="thumbnail">
      {% for carousel in carousels %}
        {% if forloop.first %}
          <div class="item active">
        {% else %}
          <div class="item">
        {% endif %}
        <img src="{{ carousel.image.url }}">
          {% if carousel.title %}
          <div class="title" style="color: {{ carousel.color }}">
              {{ carousel.title }}
          </div>
          {% endif %}
        </div>
      {% endfor %}
    </div>
  </div>
</section>
<!--圖片輪播-->
<!--最新行情-->
<section class="trading" id="trading">
  <div class="row" style="border:1px #ccc solid;padding:5px;margin-top:15px;color:white";>
      <center>
{% for row in rows %}
{% if forloop.first %}
<table>
	<caption><h2>落花生市場交易情形</h2></caption>
	<tr>
		<th>交易日期</th>
		<th>作物代號</th>
		<th>作物名稱</th>
		<th>市場代碼</th>
		<th>市場名稱</th>
		<th>上價</th>
		<th>中價</th>
		<th>下價</th>
		<th>平均價</th>
		<th>交易量</th>
	</tr>
{% endif %}
	<tr>
		<td><center>{{ row.交易日期 }}</center></td>
		<td><center>{{ row.作物代號 }}</center></td>
		<td><center>{{ row.作物名稱 }}</center></td>
		<td><center>{{ row.市場代號 }}</center></td>
		<td><center>{{ row.市場名稱 }}</center></td>
		<td><center>{{ row.上價 }}</center></td>
		<td><center>{{ row.中價 }}</center></td>
		<td><center>{{ row.下價 }}</center></td>
		<td><center>{{ row.平均價 }}</center></td>
		<td><center>{{ row.交易量 }}</center></td>
	</tr>
{% if forloop.last %}
</table>
{% endif %}
{% endfor %}
</center>
</row>
</section>
<!--最新行情-->
<!--介紹-->
{% if about %}
  <section class="about" id="about">
    <h1 class="heading"> <span>about</span> us </h1>
    <div class="row">
      <div class="image">
          <img src="{{ about.image.url }}" alt="">
      </div>
      <div class="content2">
        <h3>{{ about.title }}</h3>
        {% if about.simple_introduce %}
          <p>{{ about.simple_introduce }}</p>
        {% endif %}
        {% if about.introduce %}
          <p>{{ about.introduce }}</p>
        {% endif %}
        {% if about.link %}
        <a href="{{ about.link }}" class="btn">
          關於更多
          <span></span><span></span><span></span>
        </a>
        {% endif %}
      </div>
    </div>
  </section>
{% endif %}
<!--介紹-->

<!--產品-->
<section class="menu" id="menu">
  <h1 class="heading">our <span>partner vendors</span> </h1>
  <div class="bix-container">
    {% for parent in parents %}
      <div class="box">
        <img src="{{ parent.seller_image.url }}" alt="">
        <h3>{{ parent.seller_name }}</h3>
        <a href="{% url 'seller_detail' parent.id %}" class="btn">
          關於更多
          <span></span><span></span><span></span>
        </a>
      </div>
    {% endfor %}
  </div>
</section>
<!--產品-->
<!--商業模式模式圖-->
<section class="business" id="business">
<div class="row" style="border:0px #ccc solid;padding:5px;margin-top:15px;color:white">
<h1 class="heading">商業模式模式圖</h1>
<table style="width:100%">
  <tr>
    <td rowspan="2">關係合作夥伴</td>
    <td>關鍵活動</td>
    <td rowspan="2" colspan="2">價值主張</td>
    <td>顧客關係</td>
    <td rowspan="2">
        <img src="{% static 'assets/images/user.png' %}" width="5%">
        <h2>目標客層</h2>
        小農<br>
        店家
    </td>
  </tr>
  <tr>
    <td>關鍵資源</td>
    <td>通路</td>
  </tr>
  <tr>
    <td colspan="3">成本結構</td>
    <td colspan="3">收益流</td>
  </tr>
</table>
<div>
</section>
<!--商業模式模式圖-->
<!--聯絡-->
<section class="contact" id="contact">
  <h1 class="heading"><span>contact </span>us</h1>
  <div class="row">
    {% if mapIframe %}
      {{ mapIframe.map_iframe | safe }}
    {% endif %}
    <form method="post">
      {% csrf_token %}
      <h3>聯 絡 我 們</h3>
      <div class="inputBox">
        <span class="fa-solid fa-user"></span>
        {{ form.name }}
      </div>
      <div class="inputBox">
        <span class="fa-solid fa-envelope"></span>
        {{ form.email }}
      </div>
      <div class="inputBox">
        <span class="fa-solid fa-phone"></span>
        {{ form.phone }}
      </div>
      <div class="inputBox">
        <span class="fa-solid fa-question"></span>
        {{ form.question }}
      </div>
      <button type="submit" class="btn">
        提 交
        <span></span><span></span><span></span>
      </button>
    </form>
  </div>
</section>
<!--聯絡-->

<!--底部-->
<section class="footer">
  <div class="share">
    {% if profiles.facebook_link %}
      <a href="{{ profiles.facebook_link }}" class="fa-brands fa-facebook"></a>
    {% endif %}
    {% if profiles.instagram_link %}
      <a href="{{ profiles.instagram_link }}" class="fa-brands fa-instagram"></a>
    {% endif %}
    {% if profiles.youtube_link %}
      <a href="{{ profiles.youtube_link }}" class="fa-brands fa-youtube"></a>
    {% endif %}
  </div>
  <div class="credit">© <script>document.write(new Date().getFullYear());</script> Copyrights 尚虎雲產銷平台</br><script>document.write(new Date().getFullYear());</script> All rights reserved | This template is made with Wen Qi</div>
  {% if profiles.phone_number %}
    <div class="credit">連絡電話: {{ profiles.phone_number }}</div>
  {% endif %}
  {% if profiles.contact_address %}
    <div class="credit">連絡地址: {{ profiles.contact_address }}</div>
  {% endif %}
  {% if profiles.real_name %}
    <div class="credit">聯絡人: {{ profiles.real_name }}</div>
  {% endif %}
  {% if profiles.description %}
    <div class="credit">個人簡介: {{ profiles.description }}</div>
  {% endif %}
</section>
<!--底部-->
{% endblock container %}

{% block javascripts %}
<script>
  document.addEventListener("DOMContentLoaded", function() {
    {% if send_success %}
      alert("表單已提交");
    {% endif %}
  });

  function scroll() {
    var top = $(".bg1").offset().top; // 獲取導航欄變色的位置距頂部的高度
    var scrollTop = $(window).scrollTop(); // 獲取當前窗口距頂部的高度
    if (scrollTop <= top) {
      $('.nav').css('background-color', 'transparent');
    } else {
      $('.nav').css('background-color', '#111111');
    }
  }

  $(window).on('scroll', function() {
    scroll();
  });
</script>
<script src="{{ ASSETS_ROOT }}/js/Front page.js"></script>
{% endblock javascripts %}

2. 上傳圖示


尚虎雲產銷平台-銷售端整合市場行情


程式下載:尚虎雲產銷平台-銷售端

參考文章:尚虎雲產銷平台-銷售端軟體安裝手冊用Python/Django/pythonanywhere實作農漁業產品交易行情

上架平台:pythonanywhere

1. Sales-side/apps/templates/home/Front page.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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
{% extends "layouts/base.html" %}

{% comment %} 這個是網頁的title {% endcomment %}
{% block title %}
  {% if web_inf.title %}
    {{ web_inf.title }}
  {% else %}
    尚虎雲
  {% endif %}
{% endblock title %}

{% comment %} 在這裡加入css {% endcomment %}
{% block stylesheets %}
<link href="{{ ASSETS_ROOT }}/css/Front page.css" rel="stylesheet" media="all" autostart="true">
<style>
  .active { /* 示例樣式 */
      color: red;
  }
  .slide {
      display: inline-block;
      width: 100%;
  }
  .carousel-container {
      width: 100%;
      overflow: hidden;
  }
  .carousel-inner {
      display: flex;
      transition: margin-left 0.5s ease;
  }
  table, th, td {
  border: 1px solid white;
  border-collapse: collapse;
}
</style>
{% endblock stylesheets %}

{% block container %}
<!--首頁-->
<div class="bg1">
  <div class="nav">
    <a href="#" class="logo">
      <img src="{% if web_inf.logo %}{{ web_inf.logo.url }}{% else %}{{ ASSETS_ROOT }}/images/favicon.png{% endif %}">
    </a>
    <div class="nav-text">
      <a href="#home">主頁</a>
      <a href="#trading">交易行情</a>
      <a href="#about">關於我們</a>
      <a href="#menu">賣家</a>
      <a href="#contact">聯絡我們</a>
    </div>
  </div>
</div>
<!--首頁-->

<!--圖片輪播-->
<section class="home" id="home">
  <div class="slider">
    <!-- list Items -->
    <div class="list">
      {% for carousel in carousels %}
        {% if forloop.first %}
          <div class="item active">
        {% else %}
          <div class="item">
        {% endif %}
        <img src="{{ carousel.image.url }}">
            <div class="content" style="color: {{ carousel.color }}">
              {% if carousel.header %}
                <p>
                  {{ carousel.header }}
                </p>
              {% endif %}
              {% if carousel.title %}
                <h2>
                  {{ carousel.title }}
                </h2>
              {% endif %}

              {% if carousel.content %}
                <p>
                  {{ carousel.content }}
                </p>
              {% endif %}
            </div>
          </div>
      {% endfor %}
    </div>

    <!-- button arrows -->
    <div class="arrows">
        <button id="prev"><</button>
        <button id="next">></button>
    </div>

    <!-- thumbnail -->
    <div class="thumbnail">
      {% for carousel in carousels %}
        {% if forloop.first %}
          <div class="item active">
        {% else %}
          <div class="item">
        {% endif %}
        <img src="{{ carousel.image.url }}">
          {% if carousel.title %}
          <div class="title" style="color: {{ carousel.color }}">
              {{ carousel.title }}
          </div>
          {% endif %}
        </div>
      {% endfor %}
    </div>
  </div>
</section>
<!--圖片輪播-->
<!--最新行情-->
<section class="trading" id="trading">
  <div class="row" style="border:1px #ccc solid;padding:5px;margin-top:15px;color:white";>
      <center>
{% for row in rows %}
{% if forloop.first %}
<table>
	<caption><h2>落花生市場交易情形</h2></caption>
	<tr>
		<th>交易日期</th>
		<th>作物代號</th>
		<th>作物名稱</th>
		<th>市場代碼</th>
		<th>市場名稱</th>
		<th>上價</th>
		<th>中價</th>
		<th>下價</th>
		<th>平均價</th>
		<th>交易量</th>
	</tr>
{% endif %}
	<tr>
		<td><center>{{ row.交易日期 }}</center></td>
		<td><center>{{ row.作物代號 }}</center></td>
		<td><center>{{ row.作物名稱 }}</center></td>
		<td><center>{{ row.市場代號 }}</center></td>
		<td><center>{{ row.市場名稱 }}</center></td>
		<td><center>{{ row.上價 }}</center></td>
		<td><center>{{ row.中價 }}</center></td>
		<td><center>{{ row.下價 }}</center></td>
		<td><center>{{ row.平均價 }}</center></td>
		<td><center>{{ row.交易量 }}</center></td>
	</tr>
{% if forloop.last %}
</table>
{% endif %}
{% endfor %}
</center>
</row>
</section>
<!--最新行情-->
<!--介紹-->
{% if about %}
  <section class="about" id="about">
    <h1 class="heading"> <span>about</span> us </h1>
    <div class="row">
      <div class="image">
          <img src="{{ about.image.url }}" alt="">
      </div>
      <div class="content2">
        <h3>{{ about.title }}</h3>
        {% if about.simple_introduce %}
          <p>{{ about.simple_introduce }}</p>
        {% endif %}
        {% if about.introduce %}
          <p>{{ about.introduce }}</p>
        {% endif %}
        {% if about.link %}
        <a href="{{ about.link }}" class="btn">
          關於更多
          <span></span><span></span><span></span>
        </a>
        {% endif %}
      </div>
    </div>
  </section>
{% endif %}
<!--介紹-->

<!--產品-->
<section class="menu" id="menu">
  <h1 class="heading">our <span>partner vendors</span> </h1>
  <div class="bix-container">
    {% for parent in parents %}
      <div class="box">
        <img src="{{ parent.seller_image.url }}" alt="">
        <h3>{{ parent.seller_name }}</h3>
        <a href="{% url 'seller_detail' parent.id %}" class="btn">
          關於更多
          <span></span><span></span><span></span>
        </a>
      </div>
    {% endfor %}
  </div>
</section>
<!--產品-->

<!--聯絡-->
<section class="contact" id="contact">
  <h1 class="heading"><span>contact </span>us</h1>
  <div class="row">
    {% if mapIframe %}
      {{ mapIframe.map_iframe | safe }}
    {% endif %}
    <form method="post">
      {% csrf_token %}
      <h3>聯 絡 我 們</h3>
      <div class="inputBox">
        <span class="fa-solid fa-user"></span>
        {{ form.name }}
      </div>
      <div class="inputBox">
        <span class="fa-solid fa-envelope"></span>
        {{ form.email }}
      </div>
      <div class="inputBox">
        <span class="fa-solid fa-phone"></span>
        {{ form.phone }}
      </div>
      <div class="inputBox">
        <span class="fa-solid fa-question"></span>
        {{ form.question }}
      </div>
      <button type="submit" class="btn">
        提 交
        <span></span><span></span><span></span>
      </button>
    </form>
  </div>
</section>
<!--聯絡-->

<!--底部-->
<section class="footer">
  <div class="share">
    {% if profiles.facebook_link %}
      <a href="{{ profiles.facebook_link }}" class="fa-brands fa-facebook"></a>
    {% endif %}
    {% if profiles.instagram_link %}
      <a href="{{ profiles.instagram_link }}" class="fa-brands fa-instagram"></a>
    {% endif %}
    {% if profiles.youtube_link %}
      <a href="{{ profiles.youtube_link }}" class="fa-brands fa-youtube"></a>
    {% endif %}
  </div>
  <div class="credit">© <script>document.write(new Date().getFullYear());</script> Copyrights 尚虎雲產銷平台</br><script>document.write(new Date().getFullYear());</script> All rights reserved | This template is made with Wen Qi</div>
  {% if profiles.phone_number %}
    <div class="credit">連絡電話: {{ profiles.phone_number }}</div>
  {% endif %}
  {% if profiles.contact_address %}
    <div class="credit">連絡地址: {{ profiles.contact_address }}</div>
  {% endif %}
  {% if profiles.real_name %}
    <div class="credit">聯絡人: {{ profiles.real_name }}</div>
  {% endif %}
  {% if profiles.description %}
    <div class="credit">個人簡介: {{ profiles.description }}</div>
  {% endif %}
</section>
<!--底部-->
{% endblock container %}

{% block javascripts %}
<script>
  document.addEventListener("DOMContentLoaded", function() {
    {% if send_success %}
      alert("表單已提交");
    {% endif %}
  });

  function scroll() {
    var top = $(".bg1").offset().top; // 獲取導航欄變色的位置距頂部的高度
    var scrollTop = $(window).scrollTop(); // 獲取當前窗口距頂部的高度
    if (scrollTop <= top) {
      $('.nav').css('background-color', 'transparent');
    } else {
      $('.nav').css('background-color', '#111111');
    }
  }

  $(window).on('scroll', function() {
    scroll();
  });
</script>
<script src="{{ ASSETS_ROOT }}/js/Front page.js"></script>
{% endblock javascripts %}

2. Sales-side/apps/home/views.py程式修改

  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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import traceback
from django import template
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.template import loader
from django.urls import reverse
from django.core.mail import send_mail
from django.conf import settings
from django.shortcuts import get_object_or_404
from .models import(
    Profile,
    WebBasicInf,
    CarouselImage,
    AboutUs,
    SellerInfo,
    ProductInfo,
    ProductImage,
    MapIframe
)
from .forms import ContactForm
import requests
import json


def index(request):
    context = {}
    try:
        context['profiles'] = Profile.objects.get()
    except Profile.DoesNotExist:
        pass
    try:
        context['mapIframe'] = MapIframe.objects.get()
    except MapIframe.DoesNotExist:
        pass
    try:
        context['about'] = AboutUs.objects.get()
    except AboutUs.DoesNotExist:
        pass
    try:
        context['web_inf'] = WebBasicInf.objects.get()
    except WebBasicInf.DoesNotExist:
        pass

    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            phone = form.cleaned_data['phone']
            question = form.cleaned_data['question']

            subject = '聯絡表單提交'
            message = f'姓名: {name}\n電子郵件: {email}\n電話號碼: {phone}\n提問內容: {question}'
            email_from = settings.EMAIL_HOST_USER
            recipient_list = [email, context['profiles'].email]

            send_mail(subject, message, email_from, recipient_list)

            context["send_success"] = True

    else:
        form = ContactForm()

    context["form"] = form

    context["carousels"] = CarouselImage.objects.all()
    context["parents"] = SellerInfo.objects.all()
    r = requests.get('https://data.moa.gov.tw/Service/OpenData/FromM/FarmTransData.aspx')
    text = json.loads(r.text)
    rows = []
    for row in text:
        if row['作物名稱'] is None:
            continue
        if '落花生' in row['作物名稱']:
            rows.append(row)
    context["rows"] = rows
    html_template = loader.get_template('home/Front page.html')
    return HttpResponse(html_template.render(context, request))

def seller_detail_view(request, id):
    context = {}
    context["seller"] = get_object_or_404(SellerInfo, id=id)
    html_template = loader.get_template('home/index.html')
    return HttpResponse(html_template.render(context, request))

def pages(request):
    context = {}
    try:
        context['web_inf'] = WebBasicInf.objects.get()
    except WebBasicInf.DoesNotExist:
        pass
    try:
        load_template = request.path.split('/')[-1]

        # 後台
        if load_template == 'admin':
            return HttpResponseRedirect(reverse('admin:index'))

        # 紀錄目前是哪個模板
        html_template = loader.get_template('home/' + load_template)
        return HttpResponse(html_template.render(context, request))

    except template.TemplateDoesNotExist:
        traceback.print_exc()
        html_template = loader.get_template('home/page-404.html')
        return HttpResponse(html_template.render(context, request))

    except:
        traceback.print_exc()
        html_template = loader.get_template('home/page-500.html')
        return HttpResponse(html_template.render(context, request))


def product_list(request, seller_id):
    products = ProductInfo.objects.filter(seller__id=seller_id).values()
    return JsonResponse(list(products), safe=False)

def product_photos(request, product_id):
    images = ProductImage.objects.filter(product_id=product_id).values()
    return JsonResponse(list(images), safe=False)