Skip to content

Commit cf09bf6

Browse files
committed
more on listings in 19
1 parent 45116f9 commit cf09bf6

File tree

3 files changed

+41
-32
lines changed

3 files changed

+41
-32
lines changed

chapter_15_advanced_forms.asciidoc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,12 +843,18 @@ use Django instead, it seems like we need to bring back our custom
843843
<1> We give `.was-validated` to the `<form>`
844844
<2> We provide an `<input>` and the most important custom setting will be its
845845
`class`. As you can see, we can use conditionals even for providing
846-
additional `class` -es.
846+
additional `class` -es.footnote:[
847+
We've split the form tag across three lines so it fits nicely in the book.
848+
If you've not seen that before, it may look a little weird to you,
849+
but I promise it is valid HTML.
850+
You don't have to use it if you don't like it though. :)]
851+
847852
<3> If you try to just simply display `form.errors.text` you'll see it ends up
848853
as a list item in an unordered list. To avoid that, let's just
849854
select `form.errors.text.0`.
850855
851-
NOTE: Another fliip-flop! We spent most of the last chapter switching from handcrafted html
856+
NOTE: Another fliip-flop!
857+
We spent most of the last chapter switching from handcrafted html
852858
to having our form autogenerated by django, and now we're switching back.
853859
It's a little frustrating, and I could have gone back and changed the book's text to avoid the back+forth,
854860
but I prefer to show software development as it really is.

chapter_19_mocking.asciidoc

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ HTML. You don't have to use it if you don't like it though. :)]
528528

529529

530530
[role="sourcecode small-code"]
531-
.lists/templates/base.html (ch17l012)
531+
.lists/templates/base.html (ch19l012)
532532
====
533533
[source,html]
534534
----
@@ -686,7 +686,7 @@ need to actually add the messages to the page. Something like this:
686686

687687

688688
[role="sourcecode dofirst-ch17l014-4"]
689-
.lists/templates/base.html (ch17l015)
689+
.lists/templates/base.html (ch19l015)
690690
====
691691
[source,html]
692692
----
@@ -924,25 +924,25 @@ So we can insert the token into our email like this:
924924

925925

926926
[role="sourcecode"]
927-
.accounts/views.py (ch17l023)
927+
.accounts/views.py (ch19l023)
928928
====
929929
[source,python]
930930
----
931-
from django.core.urlresolvers import reverse
931+
from django.urls import reverse
932932
[...]
933933
934934
def send_login_email(request):
935-
email = request.POST['email']
935+
email = request.POST["email"]
936936
token = Token.objects.create(email=email)
937-
url = request.build_absolute_uri( #<1>
938-
reverse('login') + '?token=' + str(token.uid)
937+
url = request.build_absolute_uri( # <1>
938+
reverse("login") + "?token=" + str(token.uid)
939939
)
940-
message_body = f'Use this link to log in:\n\n{url}'
940+
message_body = f"Use this link to log in:\n\n{url}"
941941
send_mail(
942-
'Your login link for Superlists',
942+
"Your login link for Superlists",
943943
message_body,
944-
'noreply@superlists',
945-
[email]
944+
"noreply@superlists",
945+
[email],
946946
)
947947
[...]
948948
----
@@ -1849,18 +1849,17 @@ Let's just make sure our base template shows a different nav bar for logged-in
18491849
and non–logged-in users (which our FT relies on):
18501850

18511851
[role="sourcecode small-code"]
1852-
.lists/templates/base.html (ch17l046)
1852+
.lists/templates/base.html (ch19l046)
18531853
====
18541854
[source,html]
18551855
----
1856-
<nav class="navbar navbar-default" role="navigation">
1856+
<nav class="navbar">
18571857
<div class="container-fluid">
18581858
<a class="navbar-brand" href="/">Superlists</a>
18591859
{% if user.email %}
1860-
<ul class="nav navbar-nav navbar-right">
1861-
<li class="navbar-text">Logged in as {{ user.email }}</li>
1862-
<li><a href="#">Log out</a></li>
1863-
</ul>
1860+
<!-- TODO put in a ul -->
1861+
<span class="navbar-text">Logged in as {{ user.email }}</span>
1862+
<a href="#">Log out</a>
18641863
{% else %}
18651864
<form class="navbar-form navbar-right"
18661865
method="POST"
@@ -1912,22 +1911,23 @@ with our custom user model, as good a place to have it as any might be
19121911
====
19131912
[source,python]
19141913
----
1915-
from django.test import TestCase
19161914
from django.contrib import auth
1915+
from django.test import TestCase
1916+
19171917
from accounts.models import Token
1918+
19181919
User = auth.get_user_model()
19191920
19201921
19211922
class UserModelTest(TestCase):
1922-
19231923
def test_user_is_valid_with_email_only(self):
19241924
[...]
19251925
def test_email_is_primary_key(self):
19261926
[...]
19271927
19281928
def test_no_problem_with_auth_login(self):
1929-
user = User.objects.create(email='[email protected]')
1930-
user.backend = ''
1929+
user = User.objects.create(email="[email protected]")
1930+
user.backend = ""
19311931
request = self.client.request().wsgi_request
19321932
auth.login(request, user) # should not raise
19331933
----
@@ -1952,6 +1952,7 @@ look through the Django source lines listed in the traceback, and have a read up
19521952
of Django's https://docs.djangoproject.com/en/1.11/topics/signals/[docs on
19531953
signals].
19541954

1955+
// TODO this aint right
19551956

19561957
The upshot is that we can fix it like this:
19571958

@@ -2156,29 +2157,31 @@ http://bit.ly/SuI0hA[built-in logout view], which clears down the user's
21562157
session and redirects them to a page of our choice:
21572158

21582159
[role="sourcecode"]
2159-
.accounts/urls.py (ch17l051)
2160+
.accounts/urls.py (ch19l051)
21602161
====
21612162
[source,python]
21622163
----
2163-
from django.contrib.auth.views import logout
2164-
[...]
2164+
from django.contrib.auth import views as auth_views
2165+
from django.urls import path
2166+
2167+
from . import views
21652168
21662169
urlpatterns = [
2167-
url(r'^send_login_email$', views.send_login_email, name='send_login_email'),
2168-
url(r'^login$', views.login, name='login'),
2169-
url(r'^logout$', logout, {'next_page': '/'}, name='logout'),
2170+
path("send_login_email", views.send_login_email, name="send_login_email"),
2171+
path("login", views.login, name="login"),
2172+
path("logout", auth_views.LogoutView.as_view(), name="logout"),
21702173
]
21712174
----
21722175
====
21732176

21742177
And in 'base.html', we just make the logout into a real URL link:
21752178

21762179
[role="sourcecode"]
2177-
.lists/templates/base.html (ch17l052)
2180+
.lists/templates/base.html (ch19l052)
21782181
====
21792182
[source,python]
21802183
----
2181-
<li><a href="{% url 'logout' %}">Log out</a></li>
2184+
<a href="{% url 'logout' %}">Log out</a>
21822185
----
21832186
====
21842187

source/chapter_19_mocking/superlists

Submodule superlists updated 151 files

0 commit comments

Comments
 (0)