Skip to content

Commit

Permalink
By golly, I think things are working!
Browse files Browse the repository at this point in the history
I can't remember what all things I referenced to make this work. I've
been messing with it off and on all afternoon and evening createing and
recreating and recreating projects.

https://github.com/sloria/environs#reading-env-files

https://learndjango.com/tutorials/template-structure

evansd/whitenoise#191 (comment)
  • Loading branch information
trey committed May 4, 2023
1 parent d782266 commit e4caa41
Show file tree
Hide file tree
Showing 21 changed files with 74 additions and 39 deletions.
Empty file added __init__.py
Empty file.
57 changes: 40 additions & 17 deletions config/settings.py-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/
"""

import environs
from django.core.management.utils import get_random_secret_key
from pathlib import Path
from environs import Env

env = environs.Env()
env = Env()
# Read .env into os.environ
env.read_env()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = environs.Path(__file__).resolve(strict=True).parent.parent
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env(
"SECRET_KEY", default="{{ secret_key }}"
)
SECRET_KEY = env("SECRET_KEY", default=get_random_secret_key())

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DJANGO_DEBUG", default=False)
Expand All @@ -46,11 +48,16 @@ INSTALLED_APPS = [

# Third-party apps

INSTALLED_APPS += []
INSTALLED_APPS += [
"debug_toolbar",
"django_browser_reload",
]

# Our apps

INSTALLED_APPS += []
INSTALLED_APPS += [
"core",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
Expand All @@ -61,14 +68,18 @@ MIDDLEWARE = [
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django_browser_reload.middleware.BrowserReloadMiddleware",
]

ROOT_URLCONF = "config.urls"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [str(BASE_DIR.joinpath("templates")),],
"DIRS": [
str(BASE_DIR.joinpath("templates")),
],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
Expand All @@ -88,9 +99,10 @@ WSGI_APPLICATION = "config.wsgi.application"
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#databases

DATABASES = {
"default": env.dj_db_url("DATABASE_URL", default="postgres:///{{ project_name }}"),
"default": env.dj_db_url("DATABASE_URL", default="sqlite:///db.sqlite3"),
}

AUTH_USER_MODEL = "core.User"

# Password validation
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#auth-password-validators
Expand All @@ -99,9 +111,15 @@ AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]

# Internationalization
Expand All @@ -118,13 +136,18 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/{{ docs_version }}/howto/static-files/

STATIC_ROOT = str(BASE_DIR.joinpath("static"))
STATIC_URL = "/static/"
STATICFILES_DIRS = (str(BASE_DIR.joinpath("frontend")),)
# STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATICFILES_DIRS = [BASE_DIR / "static"]
STATIC_ROOT = BASE_DIR / "staticfiles"

STORAGES = {
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}

MEDIA_URL = "/media/"
MEDIA_ROOT = str(BASE_DIR.joinpath("media"))
MEDIA_ROOT = BASE_DIR / "media"

if DEBUG:
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
Expand Down
8 changes: 6 additions & 2 deletions config/urls.py-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path

from django.views.generic.base import TemplateView

urlpatterns = [
path("", views.index, name="index"),
# TODO: Remove this template and add your own.
path(
"", TemplateView.as_view(template_name="placeholder.html"), name="placeholder"
),
# TODO: Change the `admin/` path to something custom.
path(settings.ADMIN_URL, admin.site.urls),
]

Expand Down
Empty file added core/__init__.py-tpl
Empty file.
1 change: 1 addition & 0 deletions self-destruct/admin.py → core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from django.contrib.auth.admin import UserAdmin
from .models import User


admin.site.register(User, UserAdmin)
6 changes: 6 additions & 0 deletions core/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CoreConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "core"
Empty file added core/migrations/__init__.py
Empty file.
File renamed without changes.
3 changes: 3 additions & 0 deletions core/tests.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions core/views.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions script/generate-django-key → dev/generate-django-key
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!./.venv/bin/python

from django.core.management.utils import get_random_secret_key

print(get_random_secret_key())
14 changes: 14 additions & 0 deletions script/setup.sh → dev/setup.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/sh

# This is to initially set up a project. You probably don't need to
# ever run it again.

raw_folder=${PWD##*/} # Get the name of the current folder.
project_folder=${raw_folder//-/_} # Replace hyphens with underscores.

Expand All @@ -14,6 +17,15 @@ django-admin startproject \
$project_folder .
pip-compile --resolver=backtracking requirements/requirements.in
python -m pip install -r requirements/requirements.txt
pre-commit install
echo "DEBUG=True" >> .env
echo "ALLOWED_HOSTS=*" >> .env

# Warm up the database and static files
python manage.py collectstatic
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

# Setup JS stuff
source $HOME/.nvm/nvm.sh
Expand All @@ -22,3 +34,5 @@ npm install

# Start a new Git project
git init --initial-branch=main&&git add .&&git commit -m "New project from Piepwork's Django Starter."

echo "\nNow run `source .venv/bin/activate&&./manage.py runserver`"
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions self-destruct/tests.py

This file was deleted.

14 changes: 0 additions & 14 deletions self-destruct/urls.py

This file was deleted.

3 changes: 0 additions & 3 deletions self-destruct/views.py

This file was deleted.

File renamed without changes.

0 comments on commit e4caa41

Please sign in to comment.