Skip to content

Commit f8fd321

Browse files
JelteFmhagander
authored andcommitted
Add a generic dev uwsgi script and update readme
1 parent 432a7ca commit f8fd321

File tree

6 files changed

+89
-49
lines changed

6 files changed

+89
-49
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.pyc
2+
/env/

README.md

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,74 +14,48 @@ This is a Django 3.2 application backed by PostgreSQL and running on Python 3.x.
1414

1515
First, prepare your development environment by installing pip, virtualenv, and postgresql-server-dev-X.Y.
1616

17-
```
18-
$ sudo apt install python-pip postgresql-server-dev-14
19-
20-
$ pip install virtualenv
17+
```bash
18+
sudo apt install python-pip postgresql-server-dev-14
2119
```
2220

2321
Next, configure your local environment with virtualenv and install local dependencies.
2422

23+
```bash
24+
python3 -m venv env
25+
source env/bin/activate
26+
pip install -r dev_requirements.txt
2527
```
26-
$ virtualenv env
27-
$ source env/bin/activate
28-
$ pip install -r requirements.txt
29-
```
30-
31-
Now prepare the application to run locally.
3228

33-
Configure the app to match your local installation by creating a
34-
`local_settings.py` with the following content in the `pgcommitfest` directory.
35-
Change the values for the database connection adequately.
29+
Create a database for the application:
3630

37-
```
38-
# Enable more debugging information
39-
DEBUG = True
40-
# Prevent logging to try to send emails to postgresql.org admins.
41-
# Use the default Django logging settings instead.
42-
LOGGING = None
43-
44-
DATABASES = {
45-
'default': {
46-
'ENGINE': 'django.db.backends.postgresql_psycopg2',
47-
'NAME': 'pgcommitfest',
48-
'USER': 'postgres',
49-
'PASSWORD': 'postgres',
50-
'HOST': '0.0.0.0',
51-
}
52-
}
53-
54-
# Disables the PostgreSQL.ORG authentication.
55-
# Use the default built-in Django authentication module.
56-
AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend']
31+
```bash
32+
createdb pgcommitfest
5733
```
5834

59-
Provided that you created a database matching the above settings, you can
60-
now create the required tables. Note that a password must be provided.
35+
Create a local settings file (feel free to edit it):
6136

62-
```
63-
$ python manage.py migrate
37+
```bash
38+
cp pgcommitfest/local_settings_example.py pgcommitfest/local_settings.py
6439
```
6540

66-
You'll need either a database dump of the actual server's data or else to create a superuser:
41+
Now you can now create the required tables. Note that a password might need to
42+
be provided.
6743

68-
```
69-
$ python manage.py createsuperuser
44+
```bash
45+
./manage.py migrate
7046
```
7147

72-
Finally, you're ready to start the application:
48+
You'll need either a database dump of the actual server's data or else to create a superuser:
7349

74-
```
75-
$ python manage.py runserver
50+
```bash
51+
./manage.py createsuperuser
7652
```
7753

78-
To authenticate you'll first have to remove the customized login template.
79-
Remember not to commit this modification.
54+
Finally, you're ready to start the application:
8055

81-
```
82-
$ find . -type f -name login.html
83-
$ rm -f global_templates/admin/login.html
56+
```bash
57+
./run_dev.py
8458
```
8559

86-
Then open http://localhost:8000/admin to log in. Once redirected to the Django
60+
Then open http://localhost:8007/admin to log in. Once redirected to the Django
8761
admin interface, go back to the main interface. You're now logged in.

dev_requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-r requirements.txt
2+
uwsgi
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Enable more debugging information
2+
DEBUG = True
3+
# Prevent logging to try to send emails to postgresql.org admins.
4+
# Use the default Django logging settings instead.
5+
LOGGING = None
6+
7+
DATABASES = {
8+
'default': {
9+
'ENGINE': 'django.db.backends.postgresql_psycopg2',
10+
'NAME': 'pgcommitfest',
11+
'USER': 'postgres',
12+
'PASSWORD': 'postgres',
13+
'HOST': '0.0.0.0',
14+
}
15+
}
16+
17+
# Disables the PostgreSQL.ORG authentication.
18+
# Use the default built-in Django authentication module.
19+
AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend']

run_dev.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
"""Run uWSGI with Django static files mapping.
3+
4+
The reason we don't hardcode the path to the static admin directory in the
5+
uwsgi_dev.ini file is because the path contains the python version, something
6+
like:
7+
8+
env/lib/python3.12/site-packages/django/...
9+
10+
Requiring everyone to use the same python version is not practical, so instead
11+
we have this tiny script that will find the path to the Django admin static
12+
files and run uWSGI with the correct path.
13+
"""
14+
from importlib.machinery import PathFinder
15+
import subprocess
16+
import sys
17+
18+
django_path = PathFinder().find_spec("django").submodule_search_locations[0]
19+
20+
django_admin_path = django_path + "/contrib/admin/static/admin"
21+
22+
if len(sys.argv) > 1:
23+
ini_file = sys.argv[1]
24+
else:
25+
ini_file = "uwsgi_dev.ini"
26+
27+
subprocess.run(
28+
[
29+
"uwsgi",
30+
"--static-map",
31+
f"/media/admin={django_path}/contrib/admin/static/admin",
32+
ini_file,
33+
]
34+
)

uwsgi_dev.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[uwsgi]
2+
threads=1
3+
env=DJANGO_SETTINGS_MODULE=pgcommitfest.settings
4+
module=pgcommitfest.wsgi:application
5+
py-autoreload=1
6+
touch-reload = pgcommitfest/local_settings.py
7+
touch-reload = pgcommitfest/settings.py
8+
touch-reload = uwsgi_dev.ini
9+
http=127.0.0.1:8007
10+
static-map=/media=media

0 commit comments

Comments
 (0)