Skip to content

Commit 317c865

Browse files
authored
Port changes from the upstream startproject template (#194)
This guide is intentionally closely based around the template project generated by Django's `startproject` and `startapp` commands. This ports changes from the upstream template that were made in the last few versions of Django. The upstream template content was generated using: ``` django-admin startproject gettingstarted cd gettingstarted ./manage.py startapp hello ```
1 parent 17a1800 commit 317c865

File tree

7 files changed

+71
-32
lines changed

7 files changed

+71
-32
lines changed

gettingstarted/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for gettingstarted project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gettingstarted.settings")
15+
16+
application = get_asgi_application()

gettingstarted/settings.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,45 @@
11
"""
22
Django settings for gettingstarted project.
33
4-
Generated by 'django-admin startproject' using Django 4.0.
4+
Generated by 'django-admin startproject' using Django 4.2.1.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/4.0/topics/settings/
7+
https://docs.djangoproject.com/en/4.2/topics/settings/
88
99
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/4.0/topics/settings/
10+
https://docs.djangoproject.com/en/4.2/ref/settings/
1111
"""
1212

1313
import dj_database_url
1414
import os
1515
from django.test.runner import DiscoverRunner
1616
from pathlib import Path
1717

18-
19-
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
18+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
2019
BASE_DIR = Path(__file__).resolve().parent.parent
2120

2221

2322
IS_HEROKU = "DYNO" in os.environ
2423

2524
# Quick-start development settings - unsuitable for production
26-
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
25+
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
2726

2827
# SECURITY WARNING: keep the secret key used in production secret!
2928
SECRET_KEY = "CHANGE_ME!!!! (P.S. the SECRET_KEY environment variable will be used, if set, instead)."
3029

3130
if "SECRET_KEY" in os.environ:
3231
SECRET_KEY = os.environ["SECRET_KEY"]
3332

33+
# SECURITY WARNING: don't run with debug turned on in production!
34+
if not IS_HEROKU:
35+
DEBUG = True
3436

3537
# Generally avoid wildcards(*). However since Heroku router provides hostname validation it is ok
3638
if IS_HEROKU:
3739
ALLOWED_HOSTS = ["*"]
3840
else:
3941
ALLOWED_HOSTS = []
4042

41-
# SECURITY WARNING: don't run with debug turned on in production!
42-
if not IS_HEROKU:
43-
DEBUG = True
4443

4544
# Application definition
4645

@@ -87,14 +86,14 @@
8786

8887

8988
# Database
90-
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
89+
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
9190

9291
MAX_CONN_AGE = 600
9392

9493
DATABASES = {
9594
"default": {
9695
"ENGINE": "django.db.backends.sqlite3",
97-
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
96+
"NAME": BASE_DIR / "db.sqlite3",
9897
}
9998
}
10099

@@ -111,7 +110,7 @@
111110

112111

113112
# Password validation
114-
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
113+
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
115114

116115
AUTH_PASSWORD_VALIDATORS = [
117116
{
@@ -130,7 +129,7 @@
130129

131130

132131
# Internationalization
133-
# https://docs.djangoproject.com/en/4.0/topics/i18n/
132+
# https://docs.djangoproject.com/en/4.2/topics/i18n/
134133

135134
LANGUAGE_CODE = "en-us"
136135

@@ -142,7 +141,7 @@
142141

143142

144143
# Static files (CSS, JavaScript, Images)
145-
# https://docs.djangoproject.com/en/4.0/howto/static-files/
144+
# https://docs.djangoproject.com/en/4.2/howto/static-files/
146145

147146
STATIC_ROOT = BASE_DIR / "staticfiles"
148147
STATIC_URL = "static/"
@@ -167,6 +166,6 @@ def setup_databases(self, **kwargs):
167166

168167

169168
# Default primary key field type
170-
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
169+
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
171170

172171
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

gettingstarted/urls.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1+
"""
2+
URL configuration for gettingstarted project.
3+
4+
The `urlpatterns` list routes URLs to views. For more information please see:
5+
https://docs.djangoproject.com/en/4.2/topics/http/urls/
6+
Examples:
7+
Function views
8+
1. Add an import: from my_app import views
9+
2. Add a URL to urlpatterns: path('', views.home, name='home')
10+
Class-based views
11+
1. Add an import: from other_app.views import Home
12+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
13+
Including another URLconf
14+
1. Import the include() function: from django.urls import include, path
15+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
16+
"""
117
from django.contrib import admin
218
from django.urls import path
319

4-
admin.autodiscover()
5-
620
import hello.views
721

8-
# To add a new path, first import the app:
9-
# import blog
10-
#
11-
# Then add the new path:
12-
# path('blog/', blog.urls, name="blog")
13-
#
14-
# Learn more here: https://docs.djangoproject.com/en/2.1/topics/http/urls/
15-
1622
urlpatterns = [
1723
path("", hello.views.index, name="index"),
1824
path("db/", hello.views.db, name="db"),

gettingstarted/wsgi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
It exposes the WSGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
7+
https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/
88
"""
99

1010
import os
1111

12-
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gettingstarted.settings")
13-
1412
from django.core.wsgi import get_wsgi_application
1513

14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gettingstarted.settings")
15+
1616
application = get_wsgi_application()

hello/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class HelloConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "hello"

hello/migrations/0001_initial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 4.1.1 on 2022-09-21 16:58
1+
# Generated by Django 4.2.1 on 2023-05-18 12:17
22

33
from django.db import migrations, models
44

manage.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
23
import os
34
import sys
45

5-
if __name__ == "__main__":
6+
7+
def main():
8+
"""Run administrative tasks."""
69
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gettingstarted.settings")
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
719

8-
from django.core.management import execute_from_command_line
920

10-
execute_from_command_line(sys.argv)
21+
if __name__ == "__main__":
22+
main()

0 commit comments

Comments
 (0)