Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added 'django-admin createsuperuser' and updated tutorial to use it i…

…nstead of manually creating the user in the Python interactive prompt

git-svn-id: http://code.djangoproject.com/svn/django/trunk@261 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d330be016927bacf5fcad337138741311ac7270e 1 parent 0500321
Adrian Holovaty authored July 21, 2005
3  django/bin/django-admin.py
@@ -5,6 +5,7 @@
5 5
 
6 6
 ACTION_MAPPING = {
7 7
     'adminindex': management.get_admin_index,
  8
+    'createsuperuser': management.createsuperuser,
8 9
 #     'dbcheck': management.database_check,
9 10
     'runserver': management.runserver,
10 11
     'sql': management.get_sql_create,
@@ -63,7 +64,7 @@ def main():
63 64
         print_error("An action is required.", sys.argv[0])
64 65
     if not ACTION_MAPPING.has_key(action):
65 66
         print_error("Your action, %r, was invalid." % action, sys.argv[0])
66  
-    if action == 'init':
  67
+    if action in ('createsuperuser', 'init'):
67 68
         ACTION_MAPPING[action]()
68 69
     elif action in ('startapp', 'startproject'):
69 70
         try:
42  django/core/management.py
@@ -373,6 +373,48 @@ def startapp(app_name, directory):
373 373
 startapp.help_doc = "Creates a Django app directory structure for the given app name in the current directory."
374 374
 startapp.args = "[appname]"
375 375
 
  376
+def 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."
  416
+createsuperuser.args = ''
  417
+
376 418
 def runserver(port):
377 419
     "Starts a lightweight Web server for development."
378 420
     from django.core.servers.basehttp import run, WSGIServerException
40  docs/tutorial02.txt
@@ -10,20 +10,32 @@ application and will focus on Django's automatically-generated admin site.
10 10
 .. _Tutorial 1: http://www.djangoproject.com/documentation/tutorial1/
11 11
 
12 12
 .. admonition:: Philosophy
13  
-    
  13
+
14 14
     Generating admin sites for your staff or clients to add, change and delete
15 15
     content is tedious work that doesn't require much creativity. For that reason,
16 16
     Django entirely automates creation of admin interfaces for models.
17  
-    
  17
+
18 18
     Django was written in a newsroom environment, with a very clear separation
19 19
     between "content publishers" and the "public" site. Site managers use the
20 20
     system to add news stories, events, sports scores, etc., and that content is
21 21
     displayed on the public site. Django solves the problem of creating a unified
22 22
     interface for site administrators to edit content.
23  
-    
  23
+
24 24
     The admin isn't necessarily intended to be used by site visitors; it's for site
25 25
     managers.
26 26
 
  27
+Create a user account
  28
+=====================
  29
+
  30
+Run 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"
  35
+here. They both reference the same database.)
  36
+
  37
+The script will prompt you for a username, e-mail address and password (twice).
  38
+
27 39
 Start the development server
28 40
 ============================
29 41
 
@@ -49,28 +61,6 @@ admin's login screen:
49 61
 .. image:: http://media.djangoproject.com/img/doc/tutorial/admin01.png
50 62
    :alt: Django admin login screen
51 63
 
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()
73  
-
74 64
 Enter the admin site
75 65
 ====================
76 66
 

0 notes on commit d330be0

Please sign in to comment.
Something went wrong with that request. Please try again.