summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-11-07 02:11:27 -0800
committerCarlton Gibson <carlton.gibson@noumenal.es>2019-11-07 11:11:27 +0100
commit26554cf5d1e96db10d0d5f4b69683a22fb82fdf8 (patch)
tree57c685edd5ce651611e309e539a5eda80cd2f82b
parent77aa74cb70dd85497dbade6bc0f394aa41e88c94 (diff)
Fixed #29983 -- Replaced os.path() with pathlib.Path in project template and docs.
Thanks Curtis Maloney for the original patch.
-rw-r--r--django/conf/project_template/project_name/settings.py-tpl8
-rw-r--r--docs/howto/overriding-templates.txt6
-rw-r--r--docs/howto/static-files/index.txt2
-rw-r--r--docs/intro/tutorial02.txt4
-rw-r--r--docs/intro/tutorial07.txt2
-rw-r--r--docs/ref/contrib/gis/tutorial.txt16
-rw-r--r--docs/ref/templates/api.txt6
-rw-r--r--docs/releases/3.1.txt4
8 files changed, 24 insertions, 24 deletions
diff --git a/django/conf/project_template/project_name/settings.py-tpl b/django/conf/project_template/project_name/settings.py-tpl
index 7dfe186929..7d230ef0cc 100644
--- a/django/conf/project_template/project_name/settings.py-tpl
+++ b/django/conf/project_template/project_name/settings.py-tpl
@@ -10,10 +10,10 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/
"""
-import os
+from pathlib import Path
-# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
-BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+# Build paths inside the project like this: BASE_DIR / 'subdir'.
+BASE_DIR = Path(__file__).resolve(strict=True).parents[1]
# Quick-start development settings - unsuitable for production
@@ -76,7 +76,7 @@ WSGI_APPLICATION = '{{ project_name }}.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
- 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+ 'NAME': BASE_DIR / 'db.sqlite3',
}
}
diff --git a/docs/howto/overriding-templates.txt b/docs/howto/overriding-templates.txt
index e7c65dd354..eeba350478 100644
--- a/docs/howto/overriding-templates.txt
+++ b/docs/howto/overriding-templates.txt
@@ -27,9 +27,9 @@ Let's say you're trying to override the templates for a third-party application
called ``blog``, which provides the templates ``blog/post.html`` and
``blog/list.html``. The relevant settings for your project would look like::
- import os
+ from pathlib import Path
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+ BASE_DIR = Path(__file__).resolve(strict=True).parents[1]
INSTALLED_APPS = [
...,
@@ -40,7 +40,7 @@ called ``blog``, which provides the templates ``blog/post.html`` and
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [os.path.join(BASE_DIR, 'templates')],
+ 'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
...
},
diff --git a/docs/howto/static-files/index.txt b/docs/howto/static-files/index.txt
index 7b8366e66e..db2b520543 100644
--- a/docs/howto/static-files/index.txt
+++ b/docs/howto/static-files/index.txt
@@ -52,7 +52,7 @@ you can define a list of directories (:setting:`STATICFILES_DIRS`) in your
settings file where Django will also look for static files. For example::
STATICFILES_DIRS = [
- os.path.join(BASE_DIR, "static"),
+ BASE_DIR / "static",
'/var/www/static/',
]
diff --git a/docs/intro/tutorial02.txt b/docs/intro/tutorial02.txt
index c4fad3b78b..569e156c10 100644
--- a/docs/intro/tutorial02.txt
+++ b/docs/intro/tutorial02.txt
@@ -34,8 +34,8 @@ settings:
* :setting:`NAME` -- The name of your database. If you're using SQLite, the
database will be a file on your computer; in that case, :setting:`NAME`
should be the full absolute path, including filename, of that file. The
- default value, ``os.path.join(BASE_DIR, 'db.sqlite3')``, will store the file
- in your project directory.
+ default value, ``BASE_DIR / 'db.sqlite3'``, will store the file in your
+ project directory.
If you are not using SQLite as your database, additional settings such as
:setting:`USER`, :setting:`PASSWORD`, and :setting:`HOST` must be added.
diff --git a/docs/intro/tutorial07.txt b/docs/intro/tutorial07.txt
index 2325ac8074..60fef31da6 100644
--- a/docs/intro/tutorial07.txt
+++ b/docs/intro/tutorial07.txt
@@ -306,7 +306,7 @@ Open your settings file (:file:`mysite/settings.py`, remember) and add a
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [os.path.join(BASE_DIR, 'templates')],
+ 'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
diff --git a/docs/ref/contrib/gis/tutorial.txt b/docs/ref/contrib/gis/tutorial.txt
index 8fd8d60b62..25c2e35b1c 100644
--- a/docs/ref/contrib/gis/tutorial.txt
+++ b/docs/ref/contrib/gis/tutorial.txt
@@ -320,14 +320,12 @@ First, invoke the Django shell:
$ python manage.py shell
-If you downloaded the :ref:`worldborders` data earlier in the
-tutorial, then you can determine its path using Python's built-in
-``os`` module::
+If you downloaded the :ref:`worldborders` data earlier in the tutorial, then
+you can determine its path using Python's :class:`pathlib.Path`::
- >>> import os
+ >>> from pathlib import Path
>>> import world
- >>> world_shp = os.path.abspath(os.path.join(os.path.dirname(world.__file__),
- ... 'data', 'TM_WORLD_BORDERS-0.3.shp'))
+ >>> world_shp = Path(world.__file__).resolve().parent / 'data' / 'TM_WORLD_BORDERS-0.3.shp'
Now, open the world borders shapefile using GeoDjango's
:class:`~django.contrib.gis.gdal.DataSource` interface::
@@ -433,7 +431,7 @@ To import the data, use a LayerMapping in a Python script.
Create a file called ``load.py`` inside the ``world`` application,
with the following code::
- import os
+ from pathlib import Path
from django.contrib.gis.utils import LayerMapping
from .models import WorldBorder
@@ -452,9 +450,7 @@ with the following code::
'mpoly' : 'MULTIPOLYGON',
}
- world_shp = os.path.abspath(
- os.path.join(os.path.dirname(__file__), 'data', 'TM_WORLD_BORDERS-0.3.shp'),
- )
+ world_shp = Path(__file__).resolve().parent / 'data' / 'TM_WORLD_BORDERS-0.3.shp'
def run(verbose=True):
lm = LayerMapping(WorldBorder, world_shp, world_mapping, transform=False)
diff --git a/docs/ref/templates/api.txt b/docs/ref/templates/api.txt
index becd34c545..f9647fd83a 100644
--- a/docs/ref/templates/api.txt
+++ b/docs/ref/templates/api.txt
@@ -831,7 +831,7 @@ loaders that come with Django:
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [os.path.join(BASE_DIR, 'templates')],
+ 'DIRS': [BASE_DIR / 'templates'],
}]
You can also override ``'DIRS'`` and specify specific directories for a
@@ -843,7 +843,7 @@ loaders that come with Django:
'loaders': [
(
'django.template.loaders.filesystem.Loader',
- [os.path.join(BASE_DIR, 'templates')],
+ [BASE_DIR / 'templates'],
),
],
},
@@ -917,7 +917,7 @@ loaders that come with Django:
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [os.path.join(BASE_DIR, 'templates')],
+ 'DIRS': [BASE_DIR / 'templates'],
'OPTIONS': {
'loaders': [
('django.template.loaders.cached.Loader', [
diff --git a/docs/releases/3.1.txt b/docs/releases/3.1.txt
index 84a7e33b03..d7c05e88df 100644
--- a/docs/releases/3.1.txt
+++ b/docs/releases/3.1.txt
@@ -232,6 +232,10 @@ Miscellaneous
* The SQLite backend now supports :class:`pathlib.Path` for the ``NAME``
setting.
+* The ``settings.py`` generated by the :djadmin:`startproject` command now uses
+ :class:`pathlib.Path` instead of :mod:`os.path` for building filesystem
+ paths.
+
.. _backwards-incompatible-3.1:
Backwards incompatible changes in 3.1