You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testserver.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from django.core.management import call_command
  2. from django.core.management.base import BaseCommand
  3. from django.db import connection
  4. class Command(BaseCommand):
  5. help = 'Runs a development server with data from the given fixture(s).'
  6. requires_system_checks = False
  7. def add_arguments(self, parser):
  8. parser.add_argument(
  9. 'args', metavar='fixture', nargs='*',
  10. help='Path(s) to fixtures to load before running the server.',
  11. )
  12. parser.add_argument(
  13. '--noinput', '--no-input', action='store_false', dest='interactive',
  14. help='Tells Django to NOT prompt the user for input of any kind.',
  15. )
  16. parser.add_argument(
  17. '--addrport', default='',
  18. help='Port number or ipaddr:port to run the server on.',
  19. )
  20. parser.add_argument(
  21. '--ipv6', '-6', action='store_true', dest='use_ipv6',
  22. help='Tells Django to use an IPv6 address.',
  23. )
  24. def handle(self, *fixture_labels, **options):
  25. verbosity = options['verbosity']
  26. interactive = options['interactive']
  27. # Create a test database.
  28. db_name = connection.creation.create_test_db(verbosity=verbosity, autoclobber=not interactive, serialize=False)
  29. # Import the fixture data into the test database.
  30. call_command('loaddata', *fixture_labels, **{'verbosity': verbosity})
  31. # Run the development server. Turn off auto-reloading because it causes
  32. # a strange error -- it causes this handle() method to be called
  33. # multiple times.
  34. shutdown_message = (
  35. '\nServer stopped.\nNote that the test database, %r, has not been '
  36. 'deleted. You can explore it on your own.' % db_name
  37. )
  38. use_threading = connection.features.test_db_allows_multiple_connections
  39. call_command(
  40. 'runserver',
  41. addrport=options['addrport'],
  42. shutdown_message=shutdown_message,
  43. use_reloader=False,
  44. use_ipv6=options['use_ipv6'],
  45. use_threading=use_threading
  46. )