Skip to content

PYTHON-4814 Create project and app templates #148

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ The development version of this package supports Django 5.0.x. To install it:

`pip install git+https://github.com/mongodb-labs/django-mongodb`

### Via templates

To create a new Django project that uses MongoDB, you can use the templates in this repository. For example:

```console
$ django-admin startproject --template=project_template mysite
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should include these templates in this repo. Normally, users will be pip installing this package, which means they would either have to reference --template=/path/to/site-packages/django_mongodb/project_template or install via a URL. It looks like you modified the source distribution not to include the templates, so it's unclear how you're expecting this command to work. Probably installing via URL makes the most sense, which means we could include them in this repo and exclude them from the source, or a separate repo could work just as well. We should have separate templates for each version of Django, which could just be branches.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should include these templates in this repo. Normally, users will be pip installing this package, which means they would either have to reference --template=/path/to/site-packages/django_mongodb/project_template or install via a URL. It looks like you modified the source distribution not to include the templates, so it's unclear how you're expecting this command to work.

I'm running these local but I'm not sure either … now would be a good time to decide. I think I'm leaning slightly towards closing this PR and creating templates in a separate repository, particularly if we're going to be providing branches for each major Django release.

Probably installing via URL makes the most sense, which means we could include them in this repo and exclude them from the source, or a separate repo could work just as well. We should have separate templates for each version of Django, which could just be branches.

Agree! And the example you provided is nice. What do you think @Jibola ?

$ django-admin startapp --template=app_template polls
```

Then
```console
$ python manage.py makemigrations admin auth contenttypes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we could include the migrations in the project template.

$ python manage.py migrate
```

### Manual steps
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to make sure the readme is structured so that readers don't skip over this section since it contains a lot of useful info that's needed beyond just getting this working. For example, creating migrations and custom app configs for third-party apps they want to use.


Configure the Django `DATABASES` setting similar to this:

```python
Expand Down
Empty file added app_template/__init__.py-tpl
Empty file.
3 changes: 3 additions & 0 deletions app_template/admin.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions app_template/apps.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class {{ camel_case_app_name }}Config(AppConfig):
default_auto_field = 'django_mongodb.fields.ObjectIdAutoField'
name = '{{ app_name }}'
Empty file.
3 changes: 3 additions & 0 deletions app_template/models.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions app_template/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 app_template/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.
22 changes: 22 additions & 0 deletions project_template/manage.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file.
15 changes: 15 additions & 0 deletions project_template/project_name/apps.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.contrib.admin.apps import AdminConfig
from django.contrib.auth.apps import AuthConfig
from django.contrib.contenttypes.apps import ContentTypesConfig


class MongoAdminConfig(AdminConfig):
default_auto_field = "django_mongodb.fields.ObjectIdAutoField"


class MongoAuthConfig(AuthConfig):
default_auto_field = "django_mongodb.fields.ObjectIdAutoField"


class MongoContentTypesConfig(ContentTypesConfig):
default_auto_field = "django_mongodb.fields.ObjectIdAutoField"
16 changes: 16 additions & 0 deletions project_template/project_name/asgi.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for {{ project_name }} project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')

application = get_asgi_application()
131 changes: 131 additions & 0 deletions project_template/project_name/settings.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""
Django settings for {{ project_name }} project.

Generated by 'django-admin startproject' using Django {{ django_version }}.

For more information on this file, see
https://docs.djangoproject.com/en/{{ docs_version }}/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
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 = '{{ secret_key }}'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'{{ project_name }}.apps.MongoAdminConfig',
'{{ project_name }}.apps.MongoAuthConfig',
'{{ project_name }}.apps.MongoContentTypesConfig',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = '{{ project_name }}.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = '{{ project_name }}.wsgi.application'


# Database
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django_mongodb",
"NAME": "my_database",
# "USER": "my_username",
# "PASSWORD": "my_password",
# "OPTIONS": {...},
},
}

# Password validation
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#auth-password-validators

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',
},
]


# Internationalization
# https://docs.djangoproject.com/en/{{ docs_version }}/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/{{ docs_version }}/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django_mongodb.fields.ObjectIdAutoField'

MIGRATION_MODULES = {
'admin': '{{ project_name }}.mongo_migrations.admin',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way could be fine, but note this is configured differently than the manual steps where I put mongo_migrations in a top-level directory.

'auth': '{{ project_name }}.mongo_migrations.auth',
'contenttypes': '{{ project_name }}.mongo_migrations.contenttypes',
}
22 changes: 22 additions & 0 deletions project_template/project_name/urls.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
URL configuration for {{ project_name }} project.

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/{{ docs_version }}/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('admin/', admin.site.urls),
]
16 changes: 16 additions & 0 deletions project_template/project_name/wsgi.py-tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for {{ project_name }} project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')

application = get_wsgi_application()
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ partial_branches = ["if (.*and +)*not _use_c( and.*)*:"]

[tool.coverage.html]
directory = "htmlcov"

[tool.setuptools.packages.find]
where = ["."]
include = ["django_mongodb"]
exclude = ["docs", "project_template", "app_template"]
namespaces = false
Loading