@@ -212,7 +212,14 @@ Believe it or not, this pun didn't actually hit me until I was rewriting this ch
212
212
For 10 years it was right under my nose. I think that makes it funnier actually.]
213
213
files from Python.
214
214
215
- This is how we tell Django to enable it, in _settings.py_:
215
+ First we install whitenoise into our local environment:
216
+
217
+ [source,python]
218
+ ----
219
+ pip install whitenoise
220
+ ----
221
+
222
+ Then we tell Django to enable it, in _settings.py_:
216
223
217
224
[role="sourcecode"]
218
225
.src/superlists/settings.py (ch10l002)
@@ -263,9 +270,6 @@ Let's rerun our FTs to confirm:
263
270
// If we do that, however, we'll need to make sure readers rebuild the image each time
264
271
// we add a requirement - such as at this point.
265
272
266
-
267
- // JAN: That doesn't work until you install whitenoise to venv on local machine
268
-
269
273
[role="small-code"]
270
274
[subs="specialcharacters,macros"]
271
275
----
@@ -379,10 +383,13 @@ That's a good first cut, let's commit it:
379
383
// But I understand it's a trade-off so the reader does get bogged down.
380
384
// TODO: add a note about this and a link to some more reading
381
385
386
+ ====
387
+ [subs="specialcharacters,quotes"]
382
388
----
383
389
$ *git add requirements.txt*
384
390
$ *git commit -m "Add a requirements.txt with Django, gunicorn and whitenoise"*
385
391
----
392
+ ====
386
393
387
394
You may be wondering why we didn't add our other dependency,
388
395
Selenium, to our requirements,
@@ -452,7 +459,7 @@ TIP: Forgetting the `-r` and running `pip install requirements.txt`
452
459
// But we can cover this in a later chapter (the next one looks like a good fit,
453
460
// since it's related to the app being production ready).
454
461
455
- Let's do a build & run & test to check everything still works:
462
+ Let's build & run:
456
463
457
464
[subs="specialcharacters,quotes"]
458
465
----
@@ -462,6 +469,18 @@ $ *docker build -t superlists . && docker run \
462
469
-it superlists*
463
470
----
464
471
472
+ And then test to check everything still works:
473
+
474
+ [role="small-code"]
475
+ [subs="specialcharacters,macros"]
476
+ ----
477
+ $ pass:quotes[*TEST_SERVER=localhost:8888 python src/manage.py test functional_tests \
478
+ --failfast*]
479
+ [...]
480
+
481
+ OK
482
+ ----
483
+
465
484
=== Using Environment Variables to Adjust Settings for Production
466
485
467
486
((("DEBUG settings")))
@@ -500,9 +519,10 @@ Environment variables also have the advantage of working for non-Django stuff to
500
519
There are lots of ways you might do this.
501
520
502
521
Here's what I propose; it may seem a little fiddly,
503
- but I'll provide a little justification for each choice.
522
+ but I'll provide a little justification for each choice.
504
523
Let them be an inspiration (but not a template) for your own choices!
505
524
525
+ Note that this if statement replaces the DEBUG and SECRET_KEY lines that are included by default in the settings.py file:
506
526
507
527
[role="sourcecode"]
508
528
.superlists/settings.py (ch10l006)
@@ -749,7 +769,15 @@ CMD gunicorn --bind :8888 superlists.wsgi:application
749
769
750
770
// TODO: gitignore src/static
751
771
752
- Well, it was fiddly, but that should get us to passing tests!
772
+ Well, it was fiddly, but that should get us to passing tests after we build & run the docker container!
773
+
774
+ [subs="specialcharacters,quotes"]
775
+ ----
776
+ $ *docker build -t superlists . && docker run \
777
+ -p 8888:8888 \
778
+ --mount type=bind,source=./src/db.sqlite3,target=/src/db.sqlite3 \
779
+ -it superlists*
780
+ ----
753
781
754
782
755
783
[role="small-code"]
@@ -865,7 +893,7 @@ If you're like me you might find yourself wondering if we really _did_ see them
865
893
and starting to doubt your own sanity.
866
894
But the explanation is that Django's
867
895
https://docs.djangoproject.com/en/4.2/ref/logging/#default-logging-configuration[default logging configuration]
868
- changes when DEBUG is turned off:
896
+ changes when DEBUG is turned off.
869
897
870
898
This means we need to interact with the standard library's `logging` module,
871
899
unfortunately one of the most fiddly parts of the Python standard libraryfootnote:[
@@ -877,7 +905,7 @@ but sometimes you just wanna say "just print stuff to stdout pls",
877
905
and you wish that configuring the simplest thing was a little easier.].
878
906
879
907
Here's pretty much the simplest possible logging config
880
- which just prints everything to the console (i.e. standard out).
908
+ which just prints everything to the console (i.e. standard out). I've added this code to the very end of the settings.py file.
881
909
882
910
883
911
[role="sourcecode"]
0 commit comments