Changeset 247
- Timestamp:
- 07/20/05 12:42:36 (3 years ago)
- Files:
-
- django/trunk/django/bin/django-admin.py (modified) (2 diffs)
- django/trunk/docs/tutorial01.txt (modified) (4 diffs)
- django/trunk/docs/tutorial02.txt (modified) (1 diff)
- django/trunk/docs/tutorial03.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/bin/django-admin.py
r242 r247 1 1 #!/usr/bin/env python 2 2 from django.core import management 3 from optparse import OptionParser 3 4 import os, sys 4 5 … … 20 21 } 21 22 22 def usage(): 23 sys.stderr.write("Usage: %s [action]\n" % sys.argv[0]) 23 NO_SQL_TRANSACTION = ('adminindex', 'dbcheck', 'install', 'sqlindexes') 24 25 def get_usage(): 26 """ 27 Returns a usage string. Doesn't do the options stuff, because optparse 28 takes care of that. 29 """ 30 usage = ["usage: %prog action [options]\nactions:"] 24 31 available_actions = ACTION_MAPPING.keys() 25 32 available_actions.sort() 26 sys.stderr.write("Available actions:\n")27 33 for a in available_actions: 28 34 func = ACTION_MAPPING[a] 29 sys.stderr.write(" %s %s-- %s\n" % (a, func.args, getattr(func, 'help_doc', func.__doc__))) 35 usage.append(" %s %s-- %s" % (a, func.args, getattr(func, 'help_doc', func.__doc__))) 36 return '\n'.join(usage) 37 38 class DjangoOptionParser(OptionParser): 39 def print_usage_and_exit(self): 40 self.print_help(sys.stderr) 41 sys.exit(1) 42 43 def print_error(msg, cmd): 44 sys.stderr.write("Error: %s\nRun %s --help for help." % (msg, cmd)) 30 45 sys.exit(1) 31 46 32 if __name__ == "__main__": 47 def main(): 48 # Parse the command-line arguments. optparse handles the dirty work. 49 parser = DjangoOptionParser(get_usage()) 50 parser.add_option('--settings', 51 help='Python path to settings module, e.g. "myproject.settings.main". If this isn\'t provided, the DJANGO_SETTINGS_MODULE environment variable will be used.') 52 options, args = parser.parse_args() 53 54 # Take care of options. 55 if options.settings: 56 os.environ['DJANGO_SETTINGS_MODULE'] = options.settings 57 58 # Run the appropriate action. Unfortunately, optparse can't handle 59 # positional arguments, so this has to parse/validate them. 33 60 try: 34 action = sys.argv[1]61 action = args[0] 35 62 except IndexError: 36 usage()63 print_error("An 'action' is required.") 37 64 if not ACTION_MAPPING.has_key(action): 38 usage()65 print_error("Your 'action' was invalid.") 39 66 if action == 'init': 40 67 ACTION_MAPPING[action]() 41 sys.exit(0)42 68 elif action in ('startapp', 'startproject'): 43 69 try: 44 name = sys.argv[2]70 name = args[1] 45 71 except IndexError: 46 usage()72 parser.print_usage_and_exit() 47 73 ACTION_MAPPING[action](name, os.getcwd()) 48 sys.exit(0)49 74 elif action == 'runserver': 50 if len( sys.argv) < 3:75 if len(args) < 2: 51 76 port = '8000' 52 77 else: 53 port = sys.argv[2]78 port = args[1] 54 79 ACTION_MAPPING[action](port) 55 elif action == 'dbcheck':56 from django.core import meta57 mod_list = meta.get_all_installed_modules()58 80 else: 59 81 from django.core import meta 60 try: 61 mod_list = [meta.get_app(app_label) for app_label in sys.argv[2:]] 62 except ImportError, e: 63 sys.stderr.write("Error: %s. Are you sure your INSTALLED_APPS setting is correct?\n" % e) 64 sys.exit(1) 65 if not mod_list: 66 usage() 67 if action not in ('adminindex', 'dbcheck', 'install', 'sqlindexes'): 68 print "BEGIN;" 69 for mod in mod_list: 70 output = ACTION_MAPPING[action](mod) 71 if output: 72 print '\n'.join(output) 73 if action not in ('adminindex', 'dbcheck', 'install', 'sqlindexes'): 74 print "COMMIT;" 82 if action == 'dbcheck': 83 mod_list = meta.get_all_installed_modules() 84 else: 85 try: 86 mod_list = [meta.get_app(app_label) for app_label in args[1:]] 87 except ImportError, e: 88 sys.stderr.write("Error: %s. Are you sure your INSTALLED_APPS setting is correct?\n" % e) 89 sys.exit(1) 90 if not mod_list: 91 parser.print_usage_and_exit() 92 if action not in NO_SQL_TRANSACTION: 93 print "BEGIN;" 94 for mod in mod_list: 95 output = ACTION_MAPPING[action](mod) 96 if output: 97 print '\n'.join(output) 98 if action not in NO_SQL_TRANSACTION: 99 print "COMMIT;" 100 101 if __name__ == "__main__": 102 main() django/trunk/docs/tutorial01.txt
r212 r247 28 28 ``myproject`` directory in your current directory. 29 29 30 (``django-admin.py`` should be on your path if you installed Django via30 (``django-admin.py`` should be on your system path if you installed Django via 31 31 its setup.py utility. If it's not on your path, you can find it in 32 32 ``site-packages/django/bin``; consider symlinking to it from some place … … 68 68 immediately. 69 69 70 Once you've done that, you need to tell Django which settings module you're 71 currently using. Do that by setting an environment variable, 72 ``DJANGO_SETTINGS_MODULE``. Here's how you do that in the Bash shell on Unix:: 70 Now, take a second to make sure ``myproject`` is on your Python path. You 71 can do this by copying ``myproject`` to Python's ``site-packages`` directory, 72 or you can do it by altering the ``PYTHONPATH`` environment variable. See the 73 `Python path documentation`_ for more information. 74 75 Run the following command:: 76 77 django-admin.py init --settings='myproject.settings.main' 78 79 The ``django-admin.py`` utility generally needs to know which settings module 80 you're using. Here, we're doing that by specifying ``settings=`` on the command 81 line, but that can get tedious. If you don't want to type ``settings=`` each 82 time, you can set the ``DJANGO_SETTINGS_MODULE`` environment variable. Here's 83 how you do that in the Bash shell on Unix:: 73 84 74 85 export DJANGO_SETTINGS_MODULE=myproject.settings.main … … 78 89 set DJANGO_SETTINGS_MODULE=myproject.settings.main 79 90 80 Note this path is in Python package syntax. Your project has to be somewhere on 81 your `Python path`_ -- so that the Python statement ``import myproject.settings.main`` 82 works. Throughout Django, you'll be referring to your projects and apps via 83 Python package syntax. 84 85 Then run the following command:: 86 87 django-admin.py init 88 89 If you don't see any errors, you know it worked. That command initialized your 90 database with Django's core database tables. If you're interested, run the 91 PostgreSQL or MySQL command-line client and type "\\dt" (PostgreSQL) or 92 "SHOW TABLES;" (MySQL) to display the tables. 91 If you don't see any errors after running ``django-admin.py init``, you know it 92 worked. That command initialized your database with Django's core database 93 tables. If you're interested, run the PostgreSQL or MySQL command-line client 94 and type "\\dt" (PostgreSQL) or "SHOW TABLES;" (MySQL) to display the tables. 93 95 94 96 Now you're set to start doing work. You won't have to take care of this boring 95 97 administrative stuff again. 96 98 97 .. _`Python path `: http://docs.python.org/tut/node8.html#SECTION00811000000000000000099 .. _`Python path documentation`: http://docs.python.org/tut/node8.html#SECTION008110000000000000000 98 100 .. _Django's ticket system: http://code.djangoproject.com/report/1 99 101 … … 104 106 105 107 django-admin.py startapp polls 108 109 (From now on, this tutorial will leave out the ``--settings`` parameter and 110 will assume you've either set your ``DJANGO_SETTINGS_MODULE`` environment 111 variable or included the ``--settings`` option in your call to the command.) 106 112 107 113 That'll create a directory structure like this:: django/trunk/docs/tutorial02.txt
r219 r247 31 31 To make things easy, Django comes with a pure-Python Web server that builds on 32 32 the BaseHTTPServer included in Python's standard library. Let's start the 33 server and explore the admin site. First, set the ``DJANGO_SETTINGS_MODULE`` 34 environment variable to the location of your admin settings:: 35 36 export DJANGO_SETTINGS_MODULE=myproject.settings.admin 37 38 Then, run this command to start the server:: 39 40 django-admin.py runserver 33 server and explore the admin site. 34 35 Just run the following command to start the server:: 36 37 django-admin.py runserver --settings="myproject.settings.admin" 41 38 42 39 It'll start a Web server running locally -- on port 8000, by default. If you 43 40 want to change the server's port, pass it as a command-line argument:: 44 41 45 django-admin.py runserver 8080 42 django-admin.py runserver 8080 --settings="myproject.settings.admin" 46 43 47 44 DON'T use this server in anything resembling a production environment. It's django/trunk/docs/tutorial03.txt
r214 r247 114 114 make sure Django is following the URLconf properly. 115 115 116 Set your ``DJANGO_SETTINGS_MODULE`` environment variable to your main settings 117 (``myproject.settings.main``), as we did with the admin settings in Tutorial 2. 118 Then, fire up the Django development Web server, as we also did in Tutorial 2:: 119 120 django-admin.py runserver 116 Fire up the Django development Web server, as we did in Tutorial 2:: 117 118 django-admin.py runserver --settings="myproject.settings.admin" 121 119 122 120 Now go to "http://localhost:8000/polls/" on your domain in your Web browser.
