Commit 15 Wordbucket : add export function
Commits on Mar 30, 2018
เนื่องจาก ใช้คำเกินที่ blogger.com กำหนดจึงมีอธิบายต่อ link
/templates/detail.html
- เพิ่ม ปุ่ม export สำหรับ user ที่ logged-in
| @@ -37,6 +37,9 @@ <h2>{{ word }}</h2> |
| | {% endfor %} |
| | </table> |
| | <br> |
| | - <a href="{% url 'wordbucket:home' %}">HOME</a></td></tr> |
| | + {% if user.is_authenticated %} |
| | + <a href="{% url 'wordbucket:export' word.id %}">export</a><br><br> |
| | + {% endif %} |
| | + <a href="{% url 'wordbucket:home' %}">HOME</a> |
| | </body> |
| | </html> |
/templates/home.html
- ใส่ id สำหรับ ปุ่ม login signup และ logout เผื่อดึงมาใช้ในอนาคต
| @@ -6,10 +6,10 @@ |
| | <h1>Word Bucket</h1> |
| | {% if user.is_authenticated %} |
| | Hi {{ user.username }}! |
| | - <p><a href="{% url 'wordbucket:logout' %}">logout</a></p> |
| | + <p><a id="logout" href="{% url 'wordbucket:logout' %}">logout</a></p> |
| | {% else %} |
| | You are not logged in |
| | - <a href="{% url 'wordbucket:login' %}">LOGIN</a> or <a href="{% url 'wordbucket:signup' %}">SIGNUP</a> |
| | + <a id="login" href="{% url 'wordbucket:login' %}">LOGIN</a> or <a id="signup" href="{% url 'wordbucket:signup' %}">SIGNUP</a> |
| | <br> |
| | {% endif %} |
| | |
|
urls.py
- เพิ่ม path สำหรับ export, import(เผื่ออนาคต)
| @@ -16,4 +16,8 @@ |
| | re_path(r'^signup/$', views.signup, name='signup'), |
| | re_path(r'^login/$', auth_views.login, {'template_name': 'login.html'}, name='login'), |
| | re_path(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'), |
| | + |
| | + # csv |
| | + re_path(r'^(\d+)/import$', views.import_csv, name='import'), |
| | + re_path(r'^(\d+)/export$', views.export_csv, name='export'), |
| | ] |
views.py
- import csv สำหรับ read csv และ HttpResponse สำหรับ response ไฟล์ให้ user download
- เพิ่ม function export csv โดย query ข้อมูล explanation ต่างๆออกมาแล้ว write csv ก่อน response ให้ user
| @@ -4,6 +4,8 @@ |
| | from django.contrib.auth.forms import UserCreationForm |
| | from wordbucket.models import Word, Explanation, Like_and_dislike |
| | import string |
| | +import csv |
| | +from django.http import HttpResponse |
| | |
| | def home_page(request): |
| | d_message = "" |
| @@ -108,3 +110,24 @@ def signup(request): |
| | else: |
| | form = UserCreationForm() |
| | return render(request, 'signup.html', {'form': form}) |
| | + |
| | +# csv part |
| | + |
| | +def export_csv(request, word_id): |
| | + output = [] |
| | + word_ = Word.objects.get(id=word_id) |
| | + selected_word = word_.explanation_set.all() |
| | + response = HttpResponse(content_type='text/csv') |
| | + response['Content-Disposition'] = 'attachment; filename= "export.csv"' |
| | + |
| | + writer = csv.writer(response) |
| | + writer.writerow(['word', 'explanation']) |
| | + |
| | + for word in selected_word: |
| | + output.append([word.word, word.explanation_text]) |
| | + |
| | + writer.writerows(output) |
| | + return response |
| | + |
| | +def import_csv(request, explanation_id): |
| | + pass |
ไม่มีความคิดเห็น:
แสดงความคิดเห็น