This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart_your_own
163 lines (106 loc) · 6.92 KB
/
start_your_own
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
Kickstart Coding: Django Project Starter
This is an example start project for Kickstart Coding Django projects.
It provides a solid foundation for building a Django project that's ready to launch to Heroku or similar web-hosting service.
About
Features
Bootstrap 4: It's all set-up with Bootstrap 4 templates, a few example pages, and django-bootstrap4 for easily bootstrap-based forms
Accounts:
Log-in and sign-up pages
Custom user class
User profile page and user editing page
Heroku support: It's one git push away from publishing to the world
Pipenv: It's set-up to use Pipfile
Code standards: This was created to be a happy medium between the default django-admin starting template and heavier weight scaffolding such as Audrey / Daniel Greenfield's Django Cookiecutter, which might be better for larger projects
Easy development:
Separate local and production settings
Use Sqlite locally and postgres in production
While for bigger projects production / dev parity is vitally important, this is great for getting started with smaller projects
Misc nice stuff:
django-debug-toolbar -- great debugging tool
Error pages
Who is this for
This is for new Python/Django programmers, including coding class students who want a solid start for a Django project that uses SQLite when developing locally, and is ready to deploy Heroku provisioned Postgres database.
The documentation assumes you already have fundamental Python, Django, Bash and Heroku knowledge. If you are new to Heroku, read our Heroku Getting Started guide. If you are new to Postgres, read our Postgres Getting Started guide .
The documentation does not explicitly support Windows. It assumes you use either macOS or a GNU/Linux distribution such as Ubuntu. That said, it might work.
This was original created for Kickstart Coding, the affordable, inclusive, and intensive coding course teaching cutting-edge Python / Django and JavaScript / React web development in Oakland, CA. Learn more and enroll here.
Development
Prereq: You have Pipenv installed. You have Django admin installed globally (macOS type pip3 install django, Ubuntu GNU/Linux, type sudo pip3 install django)
Running locally
Assuming mycoolproject is the name of your project, you should start a new project using this template as follows:
django-admin startproject --template=https://github.com/kickstartcoding/django-kcproject-starter/archive/master.zip mycoolproject
Go into the newly created project, and use pipenv to get your virtualenv setup:
cd mycoolproject
pipenv shell
pipenv install --dev
Note: It's probably okay if you get errors while installing psycopg2 or gunicorn. These are not needed for development, and are likely to install fine when they are needed, e.g. on Heroku.
This starter project does not include migrations. Make migrations as such:
python manage.py makemigrations accounts
Run the migrations to actually create the SQLite database:
python manage.py migrate
Get the server running:
python manage.py runserver
Setting up with GitHub repo
Create a new repo on GitHub, and BE SURE TO NOT check "initialize with README.md" or .gitignore or anything. You want to make a truly blank repo for these steps to work without a hiccup. Just fill in the name and description, and nothing else.
Now, initialize and push the code we have by running this in the command-line:
ls # Ensure you are "next to" the manage.py, etc
git init # Initalize this
git add -A # Add all your files
git commit -m "first" # Make a commit
Finally, link up your local repository with the git one, and push. Follow the instructions provided on the github.com page. They will look a little like this, except instead of janeqhacker/mycoolproject it will be whatever your username is and your repo name is:
git remote add origin git@github.com:janeqhacker/mycoolproject.git
git push -u origin master
File structure
Directory structure description below:
[name_of_project]/
- config/
- base.py # Stores most settings
- local.py # Stores settings only for local dev
- production.py # Stores settings only used by production (e.g. Heroku)
- urls.py # Global urls.py, in turn includes urls.py in apps
- apps/ # A directory to store all our custom apps
- accounts/ # An example custom app that includes sign-up and log-in
- models.py # Customized user class is here
- urls.py # URLs for sign-up and log-in pages
- views.py # Views for sign-up and log-in pages
- forms.py # Form for editing user profile, sign-up
- templates/ # Templates for user profile stuff
- core/ # An example custom app that has some static pages
- static/ # Static files
- templates/ # Core templates, including base templates
- etc
- manage.py # Entry point
- Pipfile # Development requirements
Production
Provisioning on Heroku
Creating a new Heroku app & Postgres Database (only need to do this once per Heroku):
heroku create # Create a Heroku app (only run this if you haven't already)
heroku addons:create heroku-postgresql:hobby-dev
Optional: Confirm that the database is working by connecting to it with the command-line client (Ctrl+D to exit):
heroku pg:psql
Push to Heroku
git push heroku master
Run the migrations on the remote Postgres database. That can be done as follows:
heroku run python manage.py migrate
Your site should work! View it in your browser.
You'll also want to add a SECRET_KEY to Heroku to make your site secure:
heroku config:set SECRET_KEY=randomly hit keys
NOTE: Where it says "randomly hit keys", do just that! Just type a really long random series of letters and numbers. You'll never have to remember it again.
Going further
Mailgun-powered email
Add env variables to Heroku as such: * MAILGUN_API_KEY * MAILGUN_DOMAIN
Then, add the following to your production.py:
# Anymail (Mailgun)
# ------------------------------------------------------------------------------
# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
INSTALLED_APPS += ['anymail'] # noqa F405
EMAIL_BACKEND = 'anymail.backends.mailgun.EmailBackend'
# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
ANYMAIL = {
'MAILGUN_API_KEY': env('MAILGUN_API_KEY'),
'MAILGUN_SENDER_DOMAIN': env('MAILGUN_DOMAIN')
}
AWS S3
AWS S3 is great for handling uploaded files. Typically, this is exactly what you need: AWS supports as many and as large of files that your users might want to upload.
Use AWS:
Setup AWS and get your keys (follow this guide: https://devcenter.heroku.com/articles/s3#s3-setup)
Configure your keys using heroku config:add for each of the AWS settings specified by config/settings/production.py