Django

Code

Changeset 247

Show
Ignore:
Timestamp:
07/20/05 12:42:36 (3 years ago)
Author:
adrian
Message:

Added '--settings' option to django-admin. This specifies which settings module to use, if you don't want to deal with setting the DJANGO_SETTINGS_MODULE environment variable. Refactored django-admin to use optparse. Updated the tutorials to use '--settings' instead of environment variables, which can be confusing.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/bin/django-admin.py

    r242 r247  
    11#!/usr/bin/env python 
    22from django.core import management 
     3from optparse import OptionParser 
    34import os, sys 
    45 
     
    2021} 
    2122 
    22 def usage(): 
    23     sys.stderr.write("Usage: %s [action]\n" % sys.argv[0]) 
     23NO_SQL_TRANSACTION = ('adminindex', 'dbcheck', 'install', 'sqlindexes') 
     24 
     25def 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:"] 
    2431    available_actions = ACTION_MAPPING.keys() 
    2532    available_actions.sort() 
    26     sys.stderr.write("Available actions:\n") 
    2733    for a in available_actions: 
    2834        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 
     38class DjangoOptionParser(OptionParser): 
     39    def print_usage_and_exit(self): 
     40        self.print_help(sys.stderr) 
     41        sys.exit(1) 
     42 
     43def print_error(msg, cmd): 
     44    sys.stderr.write("Error: %s\nRun %s --help for help." % (msg, cmd)) 
    3045    sys.exit(1) 
    3146 
    32 if __name__ == "__main__": 
     47def 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. 
    3360    try: 
    34         action = sys.argv[1
     61        action = args[0
    3562    except IndexError: 
    36         usage(
     63        print_error("An 'action' is required."
    3764    if not ACTION_MAPPING.has_key(action): 
    38         usage(
     65        print_error("Your 'action' was invalid."
    3966    if action == 'init': 
    4067        ACTION_MAPPING[action]() 
    41         sys.exit(0) 
    4268    elif action in ('startapp', 'startproject'): 
    4369        try: 
    44             name = sys.argv[2
     70            name = args[1
    4571        except IndexError: 
    46             usage() 
     72            parser.print_usage_and_exit() 
    4773        ACTION_MAPPING[action](name, os.getcwd()) 
    48         sys.exit(0) 
    4974    elif action == 'runserver': 
    50         if len(sys.argv) < 3
     75        if len(args) < 2
    5176            port = '8000' 
    5277        else: 
    53             port = sys.argv[2
     78            port = args[1
    5479        ACTION_MAPPING[action](port) 
    55     elif action == 'dbcheck': 
    56         from django.core import meta 
    57         mod_list = meta.get_all_installed_modules() 
    5880    else: 
    5981        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 
     101if __name__ == "__main__": 
     102    main() 
  • django/trunk/docs/tutorial01.txt

    r212 r247  
    2828``myproject`` directory in your current directory. 
    2929 
    30 (``django-admin.py`` should be on your path if you installed Django via 
     30(``django-admin.py`` should be on your system path if you installed Django via 
    3131its setup.py utility. If it's not on your path, you can find it in 
    3232``site-packages/django/bin``; consider symlinking to it from some place 
     
    6868immediately. 
    6969 
    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:: 
     70Now, take a second to make sure ``myproject`` is on your Python path. You 
     71can do this by copying ``myproject`` to Python's ``site-packages`` directory, 
     72or you can do it by altering the ``PYTHONPATH`` environment variable. See the 
     73`Python path documentation`_ for more information. 
     74 
     75Run the following command:: 
     76 
     77    django-admin.py init --settings='myproject.settings.main' 
     78 
     79The ``django-admin.py`` utility generally needs to know which settings module 
     80you're using. Here, we're doing that by specifying ``settings=`` on the command 
     81line, but that can get tedious. If you don't want to type ``settings=`` each 
     82time, you can set the ``DJANGO_SETTINGS_MODULE`` environment variable. Here's 
     83how you do that in the Bash shell on Unix:: 
    7384 
    7485    export DJANGO_SETTINGS_MODULE=myproject.settings.main 
     
    7889    set DJANGO_SETTINGS_MODULE=myproject.settings.main 
    7990 
    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. 
     91If you don't see any errors after running ``django-admin.py init``, you know it 
     92worked. That command initialized your database with Django's core database 
     93tables. If you're interested, run the PostgreSQL or MySQL command-line client 
     94and type "\\dt" (PostgreSQL) or "SHOW TABLES;" (MySQL) to display the tables. 
    9395 
    9496Now you're set to start doing work. You won't have to take care of this boring 
    9597administrative stuff again. 
    9698 
    97 .. _`Python path`: http://docs.python.org/tut/node8.html#SECTION008110000000000000000 
     99.. _`Python path documentation`: http://docs.python.org/tut/node8.html#SECTION008110000000000000000 
    98100.. _Django's ticket system: http://code.djangoproject.com/report/1 
    99101 
     
    104106 
    105107    django-admin.py startapp polls 
     108 
     109(From now on, this tutorial will leave out the ``--settings`` parameter and 
     110will assume you've either set your ``DJANGO_SETTINGS_MODULE`` environment 
     111variable or included the ``--settings`` option in your call to the command.) 
    106112 
    107113That'll create a directory structure like this:: 
  • django/trunk/docs/tutorial02.txt

    r219 r247  
    3131To make things easy, Django comes with a pure-Python Web server that builds on 
    3232the 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 
     33server and explore the admin site. 
     34 
     35Just run the following command to start the server:: 
     36 
     37    django-admin.py runserver --settings="myproject.settings.admin" 
    4138 
    4239It'll start a Web server running locally -- on port 8000, by default. If you 
    4340want to change the server's port, pass it as a command-line argument:: 
    4441 
    45     django-admin.py runserver 8080 
     42    django-admin.py runserver 8080 --settings="myproject.settings.admin" 
    4643 
    4744DON'T use this server in anything resembling a production environment. It's 
  • django/trunk/docs/tutorial03.txt

    r214 r247  
    114114make sure Django is following the URLconf properly. 
    115115 
    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 
     116Fire up the Django development Web server, as we did in Tutorial 2:: 
     117 
     118    django-admin.py runserver --settings="myproject.settings.admin" 
    121119 
    122120Now go to "http://localhost:8000/polls/" on your domain in your Web browser.