Skip to content

Commit 0c3dd71

Browse files
committed
finish up revised second django session
1 parent 9fed7af commit 0c3dd71

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

source/presentations/session08.rst

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,3 +1594,166 @@ It would be nice if the 'author' field were auto-populated, and even hidden.
15941594
Let's do that next.
15951595

15961596

1597+
Form 'initial'
1598+
--------------
1599+
1600+
When instantiating a form, you can pass it *initial* values.
1601+
1602+
.. container:: incremental
1603+
1604+
In ``views.py`` make the following changes to the ``add_view``:
1605+
1606+
.. code-block:: python
1607+
:class: small
1608+
1609+
def add_view(request):
1610+
user = request.user
1611+
if not user.is_authenticated:
1612+
raise PermissionDenied
1613+
if request.method == 'POST':
1614+
#... not quite ready for this yet.
1615+
else:
1616+
initial = {'author': user} #<- add this
1617+
form = PostForm(initial=initial) #<- updated
1618+
1619+
1620+
Hidden Fields
1621+
-------------
1622+
1623+
If you reload, you should now see ``author`` pre-popluated.
1624+
1625+
.. container:: incremental
1626+
1627+
To hide it, we must update the 'widget' it will use in ``forms.py``:
1628+
1629+
.. code-block:: python
1630+
:class: small
1631+
1632+
class PostForm(forms.ModelForm):
1633+
1634+
class Meta:
1635+
#...
1636+
widgets = {
1637+
'author': forms.HiddenInput(),
1638+
}
1639+
1640+
.. class:: incremental
1641+
1642+
Reload again to see the input disappear. Check page source to see the 'hidden'
1643+
input.
1644+
1645+
1646+
Form Submission
1647+
---------------
1648+
1649+
That's all we need to have for processing. We want to:
1650+
1651+
.. class:: incremental
1652+
1653+
* Validate the form input
1654+
* Report validation errors to the user and return the bound form
1655+
* If no errors occur, save the form, creating an instance
1656+
* Report success to the user and redirect to the list homepage.
1657+
1658+
.. class:: incremental
1659+
1660+
Django's ``messages`` framework will allow notifications.
1661+
1662+
1663+
Handle a Submitted Form
1664+
-----------------------
1665+
1666+
In ``views.py``, update the ``add_view``:
1667+
1668+
.. code-block:: python
1669+
:class: small
1670+
1671+
def add_view(request):
1672+
user = request.user
1673+
if not user.is_authenticated:
1674+
raise PermissionDenied
1675+
if request.method == 'POST':
1676+
form = PostForm(request.POST)
1677+
if form.is_valid:
1678+
post = form.save()
1679+
msg = "post '%s' saved" % post
1680+
messages.add_message(request, messages.INFO, msg)
1681+
return HttpResponseRedirect(reverse('blog_index'))
1682+
else:
1683+
messages.add_message("please fix the errors below")
1684+
else:
1685+
#...
1686+
1687+
1688+
Showing Messages
1689+
----------------
1690+
1691+
The ``messages`` framework pushes messages onto a stack.
1692+
1693+
.. class:: incremental
1694+
1695+
You can then pop them back off by printing them in a template.
1696+
1697+
.. container:: incremental
1698+
1699+
In ``base.html`` let's give them a place to go:
1700+
1701+
.. code-block:: jinja
1702+
:class: small
1703+
1704+
<div id="container">
1705+
{% if messages %}
1706+
<div class="notifications">
1707+
{% for message in messages %}
1708+
<p>{{ message }}</p>
1709+
{% endfor %}
1710+
</div>
1711+
{% endif %}
1712+
<!-- main content div below here -->
1713+
1714+
1715+
Final Run
1716+
---------
1717+
1718+
That should be enough to get us going.
1719+
1720+
.. class:: incremental
1721+
1722+
Fill out your form, supplying title and text.
1723+
1724+
.. class:: incremental
1725+
1726+
Submit the form, and notice the messaging from the system.
1727+
1728+
.. class:: incremental
1729+
1730+
Why is your new post not appearing in the blog list?
1731+
1732+
1733+
Next Steps
1734+
----------
1735+
1736+
There are a number of improvements one could make to this blog system:
1737+
1738+
.. class:: incremental
1739+
1740+
* Send email notifications to "blog administrators" that would notify them of
1741+
new posts awaiting publication.
1742+
* Provide a second list view giving users access to edit their unpublished
1743+
posts.
1744+
* Provide restricted access to certain users to view all unpublished posts and
1745+
choose to publish them.
1746+
* Add a form field for the post category and put the post in a category when
1747+
processing the form
1748+
* Provide a list view of a category, showing all posts in it.
1749+
* Provide HTML editing for post text.
1750+
1751+
1752+
That's All For Now
1753+
------------------
1754+
1755+
But this is all we have time for in this session.
1756+
1757+
.. class:: big-centered incremental
1758+
1759+
We'll see you next session!

0 commit comments

Comments
 (0)