20👍
The migration features introduced in Django 1.7 prevents the previous answers from working on newer versions. Instead you will have to do a manual setup. Analogous to all previous answers you’ll first have to make sure Django can find your settings, and then call django.setup()
which will load the settings and setup your models. Add this to your Sphinx project’s conf.py:
os.environ['DJANGO_SETTINGS_MODULE'] = 'projectname.settings'
import django
django.setup()
14👍
Add the following to your conf.py and you will not need to set DJANGO_SETTINGS_MODULE each time:
import sys, os
sys.path.append('/path/to/your/project') # The directory that contains settings.py
# Set up the Django settings/environment
from django.core.management import setup_environ
from myproject import settings
setup_environ(settings)
12👍
With Django 1.6, I couldn’t use the answer by @MikeRyan since from django.core.management import setup_environ
has been deprecated. Instead, I went to my conf.py file and added the following:
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'dataentry.settings'
from django.conf import settings
Let me explain each line:
- I used a relative path (two directories up), but you can go ahead and put an absolute path if you’d like
- My project name is
dataentry
and the settings.py file is inside that folder; change the name (dataentry) to your project name
- Calculate point based on distance and direction
- How to make follower-following system with django model
- Django ALLOWED_HOSTS with ELB HealthCheck
- How to output text from database with line breaks in a django template?
4👍
I think you have to make Sphinx aware of the DJANGO_SETTINGS_MODULE environment variable. So do
export DJANGO_SETTINGS_MODULE=mysite.settings
(or whatever is the right value for you)
Then execute
make html
in the same terminal session.
- AttributeError: 'NoneType' object has no attribute 'attname' (Django)
- How to test a Django on_commit hook without clearing the database?
- Apache strips down "Authorization" header
- Django queryset exclude() with multiple related field clauses
0👍
Late but using Django>=1.9
and sphinx>=1.6.4
set the path equivalent to the project BASE_DIR
in the conf.py
import django
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
os.environ["DJANGO_SETTINGS_MODULE"] = "project.settings"
django.setup()
- Django + uWSGI + Nginx + SSL – request for working configuration (emphasis on SSL)
- How to download a filefield file in django view
- Django: how to change values for nullbooleanfield in a modelform?
- Django redirect() with anchor (#) parameters
- How to output text from database with line breaks in a django template?
0👍
You actually don’t need a separate settings
module. It’s sometimes easier to have one (when tests and doc share settings) but not required.
This is how dj-stripe sets up django for sphinx. The key here is the settings.configure
call with INSTALLED_APPS
as it is the only one setting key required (if your app does not require more of course):
import django
from django.conf import settings
from django.utils.encoding import force_text
from django.utils.html import strip_tags
import djstripe # noqa
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
# sys.path.insert(0, os.path.abspath('.'))
cwd = os.getcwd()
parent = os.path.dirname(cwd)
sys.path.append(parent)
settings.configure(
INSTALLED_APPS=[
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
"jsonfield",
"djstripe",
],
SITE_ID=1,
STRIPE_PUBLIC_KEY=os.environ.get("STRIPE_PUBLIC_KEY", ""),
STRIPE_SECRET_KEY=os.environ.get("STRIPE_SECRET_KEY", ""),
)
django.setup()
- Django – show loading message during long processing
- Django admin list_filter – filter field by is empty (None or empty string "")
- Django factory boy factory with OneToOne relationship and related field
- Django admin not showing models