Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctness: Not using forward slashes #54

Open
smotes opened this issue May 12, 2015 · 3 comments
Open

Correctness: Not using forward slashes #54

smotes opened this issue May 12, 2015 · 3 comments

Comments

@smotes
Copy link

smotes commented May 12, 2015

The best practice suggests using forward slashes, even while on Windows. An even better practice would be to use the build in os module to join paths together. This technique is also OS independent since it knows to use the correct directory separator. Furthermore, it's a part of the Python standard library and Django even sets up a BASE_DIR variable in your settings automatically when creating a project.

In your settings.py:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    # etc...
]
@lanshark
Copy link

I might point out that even your 'best practices' window uses backslashes in the example (cut/paste problem?) instead of the correct forward slashes...

@jcdyer
Copy link

jcdyer commented May 13, 2015

And furthermore, the anti-pattern doesn't escape the backslashes or use a raw string, which means that the actual string object doesn't have backslashes in it.

@programmdesign
Copy link
Contributor

@smotes: good point. The original idea for this pattern came from a comment in the Django docs: https://docs.djangoproject.com/en/1.8/ref/settings/#staticfiles-dirs. In their example, they seem not to have a base dir, unless you consider "/" the base dir.

@lanshark: thanks. cut/paste problem indeed, fixed
@jcdyer: valid point too, fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants