Commit 8 Wordbucket : can add explanation with word
Commits on Mar 1, 2018
- เปลี่ยน path urls.py เล็กน้องให้ไม่ต้อง localhost:8000/wordbucket/... อีกต่อไป localhost:.port../ คือหน้า link ไป app wordbucket เลย
project
| @@ -19,7 +19,6 @@ |
| | from wordbucket import urls as wordbucket_urls |
| | |
| | urlpatterns = [ |
| | - path('', views.home_page, name='home'), |
| | - path('wordbucket/', include(wordbucket_urls)), |
| | + path('', include(wordbucket_urls)), |
| | path('admin/', admin.site.urls), |
| | ] |
app.
| @@ -2,5 +2,5 @@ |
| | from wordbucket import views |
| | |
| | urlpatterns = [ |
| | - |
| | + path('', views.home_page, name='home'), |
| | ] |
- แก้ชื่ออีกเล็กน้อยเนื่องจากดัดแปลงมาจากในหนังสือจึงติดคำว่า list หรือ item มาด้วยในไฟล์ต่างๆเช่น functional_tests.py template home
- update unit test ให้ test ใส่ word ที่เชื่อมกับ explanation ไปด้วย
| @@ -13,14 +13,16 @@ def test_uses_home_template(self): |
| | self.assertTemplateUsed(response, 'home.html') |
| | |
| | def test_can_save_a_POST_request(self): |
| | - self.client.post('/', data={'word_text': 'A new list word'}) |
| | + self.client.post('/', data={'word_input': 'A new list word','explanation_input': 'yes it is'}) |
| | |
| | self.assertEqual(Word.objects.count(), 1) |
| | + self.assertEqual(Explanation.objects.count(), 1) |
| | new_word = Word.objects.first() |
| | self.assertEqual(new_word.word, 'A new list word') |
| | |
| | def test_redirects_after_POST(self): |
| | - response = self.client.post('/', data={'word_text': 'A new list word'}) |
| | + response = self.client.post('/', data={'word_input': 'A new list word','explanation_input': 'yes it is'}) |
| | + |
| | self.assertEqual(response.status_code, 302) |
| | self.assertEqual(response['location'], '/') |
| | |
- update template home ให้มีช่องใส่ explanation
| @@ -4,12 +4,14 @@ |
| | </head> |
| | <body> |
| | <h1>Word Bucket</h1> |
| | - <form method="POST"> |
| | - <input name="word_text" id="id_new_word" placeholder="Add new word" /> |
| | + <form method="POST" id="form1"> |
| | + <input name="word_input" id="id_new_word" placeholder="Add new word" /> |
| | + <input name="explanation_input" id="id_new_eplanation" placeholder="Add explanation" /> |
| | {% csrf_token %} |
| | </form> |
| | + <button type="submit" form="form1" value="Submit">Submit</button> |
| | |
| | - <table id="id_list_table"> |
| | + <table id="id_word_table"> |
| | {% for word in words %} |
| | <tr><td>{{ forloop.counter }}: {{ word.word }}</td></tr> |
| | {% endfor %} |
- update views.py ให้สร้าง explanation ที่เชื่อมต่อกับ word ด้วย
| | @@ -1,9 +1,10 @@ |
| | from django.shortcuts import redirect, render |
| | -from wordbucket.models import Word |
| | +from wordbucket.models import Word, Explanation, Like_and_dislike |
| | |
| | def home_page(request): |
| | if request.method == 'POST': |
| | - Word.objects.create(word=request.POST['word_text']) |
| | + word_ = Word.objects.create(word=request.POST['word_input']) |
| | + Explanation.objects.create(explanation_text=request.POST['explanation_input'], word=word_) |
| | return redirect('/') |
| | |
| | words = Word.objects.all() |
ไม่มีความคิดเห็น:
แสดงความคิดเห็น