I was following the instructions at
https://docs.djangoproject.com/en/1.5/intro/tutorial05/
First, when executing the command in the shell:
{{{
>>> from django.test.utils import setup_test_environment
}}}
I'm getting the following error:
{{{
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,
but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure() before
accessing settings.
}}}
This is fixable by setting a shell environment variable
{{{
export DJANGO_SETTINGS_MODULE=mysite.settings
}}}
However, when I execute the following commands from the next section in
python shell, I'm getting the results from the production database rather
than from the empty test database:
{{{
>>> response = client.get(reverse('polls:index'))
>>> response.status_code
200
>>> response.content
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20177>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
How did you start the shell? If you use `python manage.py shell`, that
should set `DJANGO_SETTINGS_MODULE`. We might want to add a reminder about
that if it's not clear.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:1>
Comment (by anonymous):
Thanks, starting the shell in this way cures the initial problem with
DJANGO_SETTINGS_MODULE, but still results come from the production
database rather than from the test database which is normally created anew
when testing.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:2>
* status: new => closed
* resolution: => needsinfo
Comment:
Did you do the second line in that first block, actually running
`setup_test_environment()` after you import it? Can you paste a full shell
session showing what you did and the results?
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:3>
Comment (by anonymous):
Here you go.
{{{
$ python manage.py shell
Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.test.utils import setup_test_environment
>>> from django.test.client import Client
>>> from django.core.urlresolvers import reverse
>>> setup_test_environment()
>>> client = Client()
>>> response = client.get(reverse('polls:index'))
>>> response.content
'\n\n<link rel="stylesheet" type="text/css" href="/static/polls/style.css"
/>\n\n\n <ul>\n \n <li><a href="/polls/1/">But
why?</a></li>\n \n </ul>\n\n'
>>>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:4>
* status: closed => new
* resolution: needsinfo =>
* stage: Unreviewed => Accepted
Comment:
Can you try if you run `setup_test_environment()` before doing `from
django.test.client import Client`? I think the latter indirectly imports
from `django.db`, meaning it sets up the database connection, meaning that
it's too late for `setup_test_environment` to change the database
settings.
I don't think this tutorial should be using `setup_test_environment` at
all. It's private API, and it's fragile; it's intended for running tests,
not for playing around in the shell. I think the testing tutorial should
just have a note "when you run this stuff in the shell, it runs against
the main database" instead. Reopening on that basis.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:5>
Comment (by timo):
Actually `setup_test_environment()` is documented and the docs suggest:
If you want to run tests outside of ./manage.py test – for example, from
a shell prompt – you will need to set up the test environment first.
Django provides a convenience method to do this
They also suggest (incorrectly, as far as I can see): "This convenience
method sets up the test database"
https://docs.djangoproject.com/en/dev/topics/testing/advanced/#running-
tests-outside-the-test-runner
That said, I agree with Carl that we probably don't need to call
`setup_test_environment` at all and should note that the shell commands
will be run against the regular database.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:6>
* cc: timograham@… (added)
* has_patch: 0 => 1
Comment:
Upon further examination, I think the `setup_test_environment` call needs
to stay. See the patch for an explanation.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:7>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"acd9dc3888aa8af881b917028062c3bdca6e610c"]:
{{{
#!CommitTicketReference repository=""
revision="acd9dc3888aa8af881b917028062c3bdca6e610c"
[1.5.x] Fixed #20177 - Corrected docs for
django.test.utils.setup_test_environment.
Thanks vlad.london.uk@ for the report.
Backport of bc02a963db from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:8>
Comment (by Tim Graham <timograham@…>):
In [changeset:"bc02a963db3aeebf7c349d83a492b6e093f42b00"]:
{{{
#!CommitTicketReference repository=""
revision="bc02a963db3aeebf7c349d83a492b6e093f42b00"
Fixed #20177 - Corrected docs for
django.test.utils.setup_test_environment.
Thanks vlad.london.uk@ for the report.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:9>
Comment (by anonymous):
Line {{{python manage.py shell}}} should be added to the tutorial, because
it is not obvious for the first time user. I had to end up here to
progress that part.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:10>
Comment (by anonymous):
You may also want to add the line
{{{
python manage.py shell
}}}
to this section of the tutorial:
[https://docs.djangoproject.com/en/1.5/intro/tutorial05/#the-django-test-
client]
Yep, I know it was already added in
[https://docs.djangoproject.com/en/1.5/intro/tutorial05/#running-tests],
but I also ended up here because I was going through the tutorial late at
night and simply typed "python" to start the console.
Thanks in advance!
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:11>
Comment (by Christophe R. Patrouch):
Perhaps another line to add is one indicates that just typing Python
rather than Python manage.py shell will result in errors,
e.g.django.core.exceptions.ImproperlyConfigured: Requested setting CACHES,
but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure() before
accessing settings.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:12>
Comment (by xpatmx):
Perhaps another line to add is one indicates that just typing Python
rather than Python manage.py shell will result in errors, e.g.
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but
settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure() before
accessing settings.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:13>
* status: closed => new
* needs_better_patch: 0 => 1
* resolution: fixed =>
* stage: Accepted => Unreviewed
Comment:
This [https://docs.djangoproject.com/en/1.6/intro/tutorial05/] needs to be
fixed. It makes readers potentially loose a lot of time and getting
irritated (and switching from learning how to run tests to watching
youbtube).
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:13>
* status: new => closed
* needs_better_patch: 1 => 0
* resolution: => fixed
* stage: Unreviewed => Accepted
Comment:
Please don't reopen semi-related tickets, which have been correctly marked
as fixed, in order to report a different issue. Adding "python manage.py
shell" to that section of the tutorial may be worth doing, but it should
have its own ticket.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:14>
Comment (by timo):
For what it's worth, we tried to address the issue in #21027 by linking
the word "shell"... perhaps it has not been entirely successful.
--
Ticket URL: <https://code.djangoproject.com/ticket/20177#comment:15>