Building SaaS (86 Part Series)
1 Building SaaS with Python and Django #1
2 Reporting scraped data – Building SaaS #2
… 82 more parts…
3 Multiple Stripe plans – Building SaaS #3
4 Using a background scheduler – Building SaaS #4
5 Updating data models – Building SaaS #5
6 Third party integration modeling – Building SaaS #6
7 Displaying third party data – Building SaaS #7
8 Connecting third party services – Building SaaS #8
9 Finishing third party integration – Building SaaS #9
10 Admin dashboards – Building SaaS #10
11 Semi-automated tasks – Building SaaS #11
12 Automation aides – Building SaaS #12
13 Deploying with Ansible – Building SaaS #13
14 Ansible Cranked to 11 – Building SaaS #14
15 Feature Flags with Django Waffle – Building SaaS #15
16 Feature Flags in Action – Building SaaS #16
17 Canceling Stripe Subscriptions – Building SaaS #17
18 Completing Account Deactivation – Building SaaS #18
19 Pip-tools and App Packaging – Building SaaS #19
20 Making a Shiv App – Building SaaS #20
21 Shiv zipapps and CI – Building SaaS #21
22 Upload to S3 with CircleCI orbs – Building SaaS #22
23 It’s Alive! A Django Shiv app – Building SaaS #23
24 In the Guts of a Shiv App – Building SaaS #24
25 It’s Permissions, Dummy! – Building SaaS #25
26 Connecting Shiv Apps with Ansible – Building SaaS #26
27 Plug the Shiv App Into Nginx – Building SaaS #27
28 Webpack and collectstatic in CI – Building SaaS #28
29 Add Static Assets to Deployment – Building SaaS #29
30 Ripping Out Node.js – Building SaaS #30
31 Celery In A Shiv App – Building SaaS #31
32 wal-e Postgres Backups – Building SaaS #32
33 Get Out, Git! – Building SaaS #33
34 Bring in the WhiteNoise, Bring in Da Funk – Building SaaS #34
35 Deploying WhiteNoise – Building SaaS #35
36 Configurama – Building SaaS #36
37 Lessons From A Failed SaaS – Building SaaS #37
38 New Project, Who Dis? – Building SaaS #38
39 django-environ and django-debug-toolbar – Building SaaS #39
40 Make A Custom User Model – Building SaaS #40
41 User Accounts With django-allauth – Building SaaS #41
42 Add Styles To Templates – Building SaaS #42
43 Use Tailwind On A Template – Building SaaS #43
44 Fast Forms With UpdateView – Building SaaS #44
45 Templates and Logic – Building SaaS #45
46 A Week At A Time – Building SaaS #46
47 How To Style Sign Up – Building SaaS #47
48 Onboarding – Building SaaS #48
49 Starting the Onboarding Flow – Building SaaS #49
50 Onboarding Continuity – Building SaaS #50
51 Onboarding Forms – Building SaaS #51
52 Consistent Onboarding – Building SaaS #52
53 More Onboarding Goodness – Building SaaS #53
54 User Testing Feedback – Building SaaS #54
55 Remodeling Data Relationships – Building SaaS #55
56 How To Fix A Bug – Building SaaS #56
57 Switch A Django Project To Use Pytest – Building SaaS #57
58 Bread and Butter Django – Building SaaS #58
59 Designing A View – Building SaaS #59
60 A View From Start To Finish – Building SaaS #60
61 Hackathon App – Building SaaS #61
62 Hackathon App Part 2 – Building SaaS #62
63 The Home Stretch – Building SaaS #63
64 Enrolling Students – Building SaaS #64
65 Docs, Bugs, and Reports – Building SaaS #66
66 Handle Default Values – Building SaaS #65
67 Give Me A Break… Day – Building SaaS #67
68 Rendering Calendars – Building SaaS #68
69 Polishing and Usability – Building SaaS #69
70 Predicting The Future – Building SaaS #70
71 Custom Form Validation – Building SaaS #71
72 Displaying Breaks – Building SaaS #72
73 Dynamically Regrouping QuerySets In Templates – Building SaaS #73
74 Check Web App Security With Bandit – Building SaaS #74
75 Make A Landing Page – Building SaaS #75
76 Capped Social Network – Building SaaS #76
77 Sending Invites – Building SaaS #77
78 Create A Form Template – Building SaaS #78
79 WhiteNoise Shenanigans – Building SaaS #79
80 Reordering Models – Building SaaS #80
81 Finishing Onboarding – Building SaaS #81
82 Customer Feedback – Building SaaS #82
83 Empty States – Building SaaS #83
84 Refactoring Enrollment – Building SaaS #84
85 Customer Docs – Building SaaS #85
86 Testing Email Designs – Building SaaS #86
In this episode, we set up a couple of tools that will be at the base of development. The first tool was django-environ to control Django settings from environment variables. The second tool was the django-debug-toolbar to help with debugging pages in future work.
We started the stream with an upgrade to Django 3.0.1 because of a security release that was announced today. For a new project, I don’t recommend upgrading packages all the time, but security releases are my exception to that rule.
After that, we installed django-environ in requirements.in
.
django-environ==0.4.5
Then installed it into the virtual environment.
(venv) $ pip-compile --output-file=requirements.txt requirements.in
(venv) $ pip install -r requirements.txt
django-environ lets us control settings from environment variables. We’ll use it much more in the future, but, for this stream, I wanted to control the DEBUG
and SECRET_KEY
settings to help secure the Heroku deployment.
In the project’s settings.py
file, we added:
import environ
# <snip other settings>
env = environ.Env(DEBUG=(bool, False))
env_file = os.path.join(BASE_DIR, ".env")
environ.Env.read_env(env_file)
# <snip>
SECRET_KEY = env("SECRET_KEY")
DEBUG = env("DEBUG")
Because the env
object received a default DEBUG
, that environment variable is optional. By declaring SECRET_KEY
without a default, the application will fail if the SECRET_KEY
environment variable is not defined.
We’re using django-environ because Heroku works by communicating settings and secrets (set via the Heroku dashboard) as environment variables available to the Heroku dyno that runs the app.
The process of adding this tool showed a couple of things:
- To use an
.env
file at the project root, we needed to explicitly provide the path. Without the path, it attempted to find an.env
file within the project’s package. - You can set the cast function to coerce an environment variable string into a data type of your choice. For instance
DEBUG=(bool, False)
tries to use thebool
function to cast a string to a boolean value. What was surprising was that strings don’t follow Python truthiness rules and only evaluate toTrue
for certain strings.
The second package that we installed in requirements.in
was django-debug-toolbar.
django-debug-toolbar==2.1
The debug toolbar is a useful tool for analyzing aspects of a Django view (like templates used, context, or database queries).
We configured the toolbar so that it will only appear in debug mode. To be clear, I didn’t intuit how to do this. We followed the installation documentation.
In settings.py
:
# Enable the debug toolbar only in DEBUG mode. if DEBUG and DEBUG_TOOLBAR:
INSTALLED_APPS.append("debug_toolbar")
MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware")
INTERNAL_IPS = ["127.0.0.1"]
and in urls.py
:
# Enable the debug toolbar only in DEBUG mode. if settings.DEBUG and settings.DEBUG_TOOLBAR:
import debug_toolbar
urlpatterns = [path("__debug__/", include(debug_toolbar.urls))] \
+ urlpatterns
We also took the step to make the toolbar configurable with a DEBUG_TOOLBAR
setting because I noticed some poor performance on some requests coming from Gunicorn, and I didn’t want to dig into the core problem.
To cap off the night, I deployed to Heroku again to show how the new settings were required as environment variables.
I generated a new secret key using the tools that come with Django.
>>> from django.core.management import utils
>>> utils.get_random_secret_key()
'3dg^)!ijawvq_%#c^6pav)#kpn4jc4t$7lgzb@=75$c+5+6r2q' # This is an example!
Now that we have some basic tooling in place, the next stream will focus on adding accounts with django-allauth.
This article first appeared on mattlayman.com.
Building SaaS (86 Part Series)
1 Building SaaS with Python and Django #1
2 Reporting scraped data – Building SaaS #2
… 82 more parts…
3 Multiple Stripe plans – Building SaaS #3
4 Using a background scheduler – Building SaaS #4
5 Updating data models – Building SaaS #5
6 Third party integration modeling – Building SaaS #6
7 Displaying third party data – Building SaaS #7
8 Connecting third party services – Building SaaS #8
9 Finishing third party integration – Building SaaS #9
10 Admin dashboards – Building SaaS #10
11 Semi-automated tasks – Building SaaS #11
12 Automation aides – Building SaaS #12
13 Deploying with Ansible – Building SaaS #13
14 Ansible Cranked to 11 – Building SaaS #14
15 Feature Flags with Django Waffle – Building SaaS #15
16 Feature Flags in Action – Building SaaS #16
17 Canceling Stripe Subscriptions – Building SaaS #17
18 Completing Account Deactivation – Building SaaS #18
19 Pip-tools and App Packaging – Building SaaS #19
20 Making a Shiv App – Building SaaS #20
21 Shiv zipapps and CI – Building SaaS #21
22 Upload to S3 with CircleCI orbs – Building SaaS #22
23 It’s Alive! A Django Shiv app – Building SaaS #23
24 In the Guts of a Shiv App – Building SaaS #24
25 It’s Permissions, Dummy! – Building SaaS #25
26 Connecting Shiv Apps with Ansible – Building SaaS #26
27 Plug the Shiv App Into Nginx – Building SaaS #27
28 Webpack and collectstatic in CI – Building SaaS #28
29 Add Static Assets to Deployment – Building SaaS #29
30 Ripping Out Node.js – Building SaaS #30
31 Celery In A Shiv App – Building SaaS #31
32 wal-e Postgres Backups – Building SaaS #32
33 Get Out, Git! – Building SaaS #33
34 Bring in the WhiteNoise, Bring in Da Funk – Building SaaS #34
35 Deploying WhiteNoise – Building SaaS #35
36 Configurama – Building SaaS #36
37 Lessons From A Failed SaaS – Building SaaS #37
38 New Project, Who Dis? – Building SaaS #38
39 django-environ and django-debug-toolbar – Building SaaS #39
40 Make A Custom User Model – Building SaaS #40
41 User Accounts With django-allauth – Building SaaS #41
42 Add Styles To Templates – Building SaaS #42
43 Use Tailwind On A Template – Building SaaS #43
44 Fast Forms With UpdateView – Building SaaS #44
45 Templates and Logic – Building SaaS #45
46 A Week At A Time – Building SaaS #46
47 How To Style Sign Up – Building SaaS #47
48 Onboarding – Building SaaS #48
49 Starting the Onboarding Flow – Building SaaS #49
50 Onboarding Continuity – Building SaaS #50
51 Onboarding Forms – Building SaaS #51
52 Consistent Onboarding – Building SaaS #52
53 More Onboarding Goodness – Building SaaS #53
54 User Testing Feedback – Building SaaS #54
55 Remodeling Data Relationships – Building SaaS #55
56 How To Fix A Bug – Building SaaS #56
57 Switch A Django Project To Use Pytest – Building SaaS #57
58 Bread and Butter Django – Building SaaS #58
59 Designing A View – Building SaaS #59
60 A View From Start To Finish – Building SaaS #60
61 Hackathon App – Building SaaS #61
62 Hackathon App Part 2 – Building SaaS #62
63 The Home Stretch – Building SaaS #63
64 Enrolling Students – Building SaaS #64
65 Docs, Bugs, and Reports – Building SaaS #66
66 Handle Default Values – Building SaaS #65
67 Give Me A Break… Day – Building SaaS #67
68 Rendering Calendars – Building SaaS #68
69 Polishing and Usability – Building SaaS #69
70 Predicting The Future – Building SaaS #70
71 Custom Form Validation – Building SaaS #71
72 Displaying Breaks – Building SaaS #72
73 Dynamically Regrouping QuerySets In Templates – Building SaaS #73
74 Check Web App Security With Bandit – Building SaaS #74
75 Make A Landing Page – Building SaaS #75
76 Capped Social Network – Building SaaS #76
77 Sending Invites – Building SaaS #77
78 Create A Form Template – Building SaaS #78
79 WhiteNoise Shenanigans – Building SaaS #79
80 Reordering Models – Building SaaS #80
81 Finishing Onboarding – Building SaaS #81
82 Customer Feedback – Building SaaS #82
83 Empty States – Building SaaS #83
84 Refactoring Enrollment – Building SaaS #84
85 Customer Docs – Building SaaS #85
86 Testing Email Designs – Building SaaS #86
原文链接:django-environ and django-debug-toolbar – Building SaaS #39
暂无评论内容