Fakultas Ilmu Komputer UI
Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
PMPL
Collection of Practice
2019
1406568923-practice
Commits
6acea56d
Commit
6acea56d
authored
Oct 03, 2019
by
emil farisan
Browse files
Redirect after POST, and show all items in template
parent
dc5d3293
Changes
3
Hide whitespace changes
Inline
Side-by-side
lists/templates/home.html
View file @
6acea56d
...
@@ -10,7 +10,9 @@
...
@@ -10,7 +10,9 @@
</form>
</form>
<table
id=
"id_list_table"
>
<table
id=
"id_list_table"
>
<tr><td>
1: {{ new_item_text }}
</td></tr>
{% for item in items %}
<tr><td>
{{ forloop.counter }}: {{ item.text }}
</td></tr>
{% endfor %}
</table>
</table>
</body>
</body>
</html>
</html>
\ No newline at end of file
lists/tests.py
View file @
6acea56d
...
@@ -8,9 +8,16 @@ class HomePageTest(TestCase):
...
@@ -8,9 +8,16 @@ class HomePageTest(TestCase):
self
.
assertTemplateUsed
(
response
,
'home.html'
)
self
.
assertTemplateUsed
(
response
,
'home.html'
)
def
test_can_save_a_POST_request
(
self
):
def
test_can_save_a_POST_request
(
self
):
self
.
client
.
post
(
'/'
,
data
=
{
'item_text'
:
'A new list item'
})
self
.
assertEqual
(
Item
.
objects
.
count
(),
1
)
new_item
=
Item
.
objects
.
first
()
self
.
assertEqual
(
new_item
.
text
,
'A new list item'
)
def
test_redirects_after_POST
(
self
):
response
=
self
.
client
.
post
(
'/'
,
data
=
{
'item_text'
:
'A new list item'
})
response
=
self
.
client
.
post
(
'/'
,
data
=
{
'item_text'
:
'A new list item'
})
self
.
assert
In
(
'A new list item'
,
response
.
content
.
decode
()
)
self
.
assert
Equal
(
response
.
status_code
,
302
)
self
.
assert
TemplateUsed
(
response
,
'home.html
'
)
self
.
assert
Equal
(
response
[
'location'
],
'/
'
)
def
test_home_page_returns_correct_html
(
self
):
def
test_home_page_returns_correct_html
(
self
):
response
=
self
.
client
.
get
(
'/'
)
response
=
self
.
client
.
get
(
'/'
)
...
@@ -20,6 +27,19 @@ class HomePageTest(TestCase):
...
@@ -20,6 +27,19 @@ class HomePageTest(TestCase):
self
.
assertTrue
(
html
.
strip
().
endswith
(
'</html>'
))
self
.
assertTrue
(
html
.
strip
().
endswith
(
'</html>'
))
self
.
assertTemplateUsed
(
response
,
'home.html'
)
self
.
assertTemplateUsed
(
response
,
'home.html'
)
def
test_only_saves_items_when_necessary
(
self
):
self
.
client
.
get
(
'/'
)
self
.
assertEqual
(
Item
.
objects
.
count
(),
0
)
def
test_displays_all_list_items
(
self
):
Item
.
objects
.
create
(
text
=
'itemey 1'
)
Item
.
objects
.
create
(
text
=
'itemey 2'
)
response
=
self
.
client
.
get
(
'/'
)
self
.
assertIn
(
'itemey 1'
,
response
.
content
.
decode
())
self
.
assertIn
(
'itemey 2'
,
response
.
content
.
decode
())
class
ItemModelTest
(
TestCase
):
class
ItemModelTest
(
TestCase
):
...
...
lists/views.py
View file @
6acea56d
from
django.shortcuts
import
render
from
django.shortcuts
import
redirect
,
render
from
lists.models
import
Item
def
home_page
(
request
):
def
home_page
(
request
):
return
render
(
request
,
'home.html'
,
{
if
request
.
method
==
'POST'
:
'new_item_text'
:
request
.
POST
.
get
(
'item_text'
,
''
),
Item
.
objects
.
create
(
text
=
request
.
POST
[
'item_text'
])
})
return
redirect
(
'/'
)
items
=
Item
.
objects
.
all
()
return
render
(
request
,
'home.html'
,
{
'items'
:
items
})
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment