Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#29983 closed Cleanup/optimization (fixed)

Replace os.path with pathlib.Path in project template and docs

Reported by: thewhisperinyourears Owned by: Jon Dufresne
Component: Core (Other) Version: 2.0
Severity: Normal Keywords: paths, settings
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Apparently there is an issue hen you are using linux (Ubuntu) and copy and paste your project to Windows. The Paths don't work. I had to change the settings in my STATICFILES_DIRS from os.path.join(BASE_DIR, 'myapp/static') to os.path.join(BASE_DIR, 'myapp', 'static')

Change History (15)

comment:1 by Curtis Maloney, 5 years ago

Owner: changed from nobody to Curtis Maloney
Status: newassigned
Triage Stage: UnreviewedAccepted

I was helping this user on IRC, and they were getting confusing errors when running "collectstatic" or "findstatic" about the path not matching.

It turned out to be a conflict with joining Windows-style paths with Unix-style paths, as mentioned above.

I'm going to look at replacing uses of os.path.join and its ilk with pathlib, which does not suffer this problem.

comment:2 by Tim Graham, 5 years ago

I'm surprised this hasn't come up before. What does the error look like?

The documentation for STATICFILES_DIRS days, "these paths should use Unix-style forward slashes, even on Windows". I guess the problem is that the way BASE_DIR is generated on Windows using backslashes. It's not clear to me what Django can do about this but we'll see what Curtis comes up with.

comment:3 by Curtis Maloney, 5 years ago

Has patch: set
Version 0, edited 5 years ago by Curtis Maloney (next)

comment:4 by Tim Graham, 5 years ago

Component: contrib.staticfilesCore (Other)
Patch needs improvement: set
Summary: Paths when using Windows from an Ubuntu projectReplace os.path with pathlib.Path in project template and docs
Type: BugCleanup/optimization

The patch doesn't work because os.path.join(BASE_DIR, 'db.sqlite3') returns a string while BASE_DIR / 'db.sqlite3' returns a PosixPath. The documentation would have to use str(BASE_DIR / 'db.sqlite3') everywhere or Django would have to convert PosixPath to string.

comment:5 by Jon Dufresne, 4 years ago

Patch needs improvement: unset

I have resurrected the original patch with a fix for SQLite.

PR

comment:6 by Claude Paroz, 4 years ago

I'm not sure if various Django settings requiring a path (template DIRS, STATIC/MEDIA_ROOT, etc.) all support path inputs. Shouldn't we check and add tests for those before changing the project settings template?

comment:7 by Carlton Gibson, 4 years ago

Needs tests: set
Owner: changed from Curtis Maloney to Jon Dufresne

Assigning Jon, since he's picked this up; marking as needs tests, since I think Claude is right, we have had regressions from moving to Path.

comment:8 by Carlton Gibson <carlton.gibson@…>, 4 years ago

In 92e5abd7:

Refs #29983 -- Added test for FIXTURES_DIRS pathlib support.

comment:9 by Carlton Gibson <carlton.gibson@…>, 4 years ago

In 1c4c68d:

Refs #29983 -- Added test for FILE_UPLOAD_TEMP_DIR pathlib support.

comment:10 by Carlton Gibson <carlton.gibson@…>, 4 years ago

In fbbff7f:

Refs #29983 -- Added pathlib.Path support to the file email backend.

comment:11 by Carlton Gibson <carlton.gibson@…>, 4 years ago

In 74f2a58b:

Refs #29983 -- Added tests for FileBasedCache pathlib support.

comment:12 by Carlton Gibson <carlton.gibson@…>, 4 years ago

In 77aa74c:

Refs #29983 -- Added support for using pathlib.Path in all settings.

comment:13 by Carlton Gibson, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:14 by Carlton Gibson <carlton.gibson@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 26554cf5:

Fixed #29983 -- Replaced os.path() with pathlib.Path in project template and docs.

Thanks Curtis Maloney for the original patch.

comment:15 by Carlton Gibson <carlton@…>, 4 years ago

In 150c3d13:

Refs #29983 -- Fixed displaying pathlib.Path database name in flush command's inputs.

Note: See TracTickets for help on using tickets.
Back to Top