Django

Code

Changeset 261

Show
Ignore:
Timestamp:
07/20/05 21:17:45 (3 years ago)
Author:
adrian
Message:

Added 'django-admin createsuperuser' and updated tutorial to use it instead of manually creating the user in the Python interactive prompt

Files:

Legend:

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

    r252 r261  
    66ACTION_MAPPING = { 
    77    'adminindex': management.get_admin_index, 
     8    'createsuperuser': management.createsuperuser, 
    89#     'dbcheck': management.database_check, 
    910    'runserver': management.runserver, 
     
    6465    if not ACTION_MAPPING.has_key(action): 
    6566        print_error("Your action, %r, was invalid." % action, sys.argv[0]) 
    66     if action == 'init'
     67    if action in ('createsuperuser', 'init')
    6768        ACTION_MAPPING[action]() 
    6869    elif action in ('startapp', 'startproject'): 
  • django/trunk/django/core/management.py

    r256 r261  
    374374startapp.args = "[appname]" 
    375375 
     376def createsuperuser(): 
     377    "Creates a superuser account." 
     378    from django.core import validators 
     379    from django.models.auth import users 
     380    import getpass 
     381    try: 
     382        while 1: 
     383            username = raw_input('Username (only letters, digits and underscores): ') 
     384            if not username.isalnum(): 
     385                sys.stderr.write("Error: That username is invalid.\n") 
     386                continue 
     387            try: 
     388                users.get_object(username__exact=username) 
     389            except users.UserDoesNotExist: 
     390                break 
     391            else: 
     392                sys.stderr.write("Error: That username is already taken.\n") 
     393        while 1: 
     394            email = raw_input('E-mail address: ') 
     395            try: 
     396                validators.isValidEmail(email, None) 
     397            except validators.ValidationError: 
     398                sys.stderr.write("Error: That e-mail address is invalid.\n") 
     399            else: 
     400                break 
     401        while 1: 
     402            password = getpass.getpass() 
     403            password2 = getpass.getpass('Password (again): ') 
     404            if password == password2: 
     405                break 
     406            sys.stderr.write("Error: Your passwords didn't match.\n") 
     407    except KeyboardInterrupt: 
     408        sys.stderr.write("\nOperation cancelled.\n") 
     409        sys.exit(1) 
     410    u = users.create_user(username, email, password) 
     411    u.is_staff = True 
     412    u.is_active = True 
     413    u.is_superuser = True 
     414    u.save() 
     415    print "User created successfully." 
     416createsuperuser.args = '' 
     417 
    376418def runserver(port): 
    377419    "Starts a lightweight Web server for development." 
  • django/trunk/docs/tutorial02.txt

    r250 r261  
    1111 
    1212.. admonition:: Philosophy 
    13      
     13 
    1414    Generating admin sites for your staff or clients to add, change and delete 
    1515    content is tedious work that doesn't require much creativity. For that reason, 
    1616    Django entirely automates creation of admin interfaces for models. 
    17      
     17 
    1818    Django was written in a newsroom environment, with a very clear separation 
    1919    between "content publishers" and the "public" site. Site managers use the 
     
    2121    displayed on the public site. Django solves the problem of creating a unified 
    2222    interface for site administrators to edit content. 
    23      
     23 
    2424    The admin isn't necessarily intended to be used by site visitors; it's for site 
    2525    managers. 
     26 
     27Create a user account 
     28===================== 
     29 
     30Run the following command to create a superuser account for your admin site:: 
     31 
     32    django-admin.py createsuperuser --settings="myproject.settings.main" 
     33 
     34(Note: You can use either "myproject.settings.main" or "myproject.settings.admin" 
     35here. They both reference the same database.) 
     36 
     37The script will prompt you for a username, e-mail address and password (twice). 
    2638 
    2739Start the development server 
     
    4961.. image:: http://media.djangoproject.com/img/doc/tutorial/admin01.png 
    5062   :alt: Django admin login screen 
    51  
    52 Create a user account 
    53 ===================== 
    54  
    55 You can't log in, though, because you haven't created an admin user account 
    56 yet. Drop into the Python interactive interpreter and type this:: 
    57  
    58     # The function django.models.auth.users.create_user() creates a new user 
    59     # and returns the new auth.User object. 
    60     # Don't use 'username' and 'password'. Those are just examples. 
    61     >>> from django.models.auth import users 
    62     >>> u = users.create_user('username', 'your_email@domain.com', 'password') 
    63  
    64     # But we're not done. We need to explicitly set is_staff and is_active to 
    65     # allow this user to access the admin. Might as well make it a superuser, 
    66     # too. 
    67     u.is_staff = True 
    68     u.is_active = True 
    69     u.is_superuser = True 
    70  
    71     # Remember, call the save() method to save changes. 
    72     u.save() 
    7363 
    7464Enter the admin site