1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Added 'django-admin createsuperuser' and updated tutorial to use it instead 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
This commit is contained in:
Adrian Holovaty
2005-07-21 02:17:45 +00:00
parent 0500321a4b
commit d330be0169
3 changed files with 59 additions and 26 deletions

View File

@@ -5,6 +5,7 @@ import os, sys
ACTION_MAPPING = {
'adminindex': management.get_admin_index,
'createsuperuser': management.createsuperuser,
# 'dbcheck': management.database_check,
'runserver': management.runserver,
'sql': management.get_sql_create,
@@ -63,7 +64,7 @@ def main():
print_error("An action is required.", sys.argv[0])
if not ACTION_MAPPING.has_key(action):
print_error("Your action, %r, was invalid." % action, sys.argv[0])
if action == 'init':
if action in ('createsuperuser', 'init'):
ACTION_MAPPING[action]()
elif action in ('startapp', 'startproject'):
try:

View File

@@ -373,6 +373,48 @@ def startapp(app_name, directory):
startapp.help_doc = "Creates a Django app directory structure for the given app name in the current directory."
startapp.args = "[appname]"
def createsuperuser():
"Creates a superuser account."
from django.core import validators
from django.models.auth import users
import getpass
try:
while 1:
username = raw_input('Username (only letters, digits and underscores): ')
if not username.isalnum():
sys.stderr.write("Error: That username is invalid.\n")
continue
try:
users.get_object(username__exact=username)
except users.UserDoesNotExist:
break
else:
sys.stderr.write("Error: That username is already taken.\n")
while 1:
email = raw_input('E-mail address: ')
try:
validators.isValidEmail(email, None)
except validators.ValidationError:
sys.stderr.write("Error: That e-mail address is invalid.\n")
else:
break
while 1:
password = getpass.getpass()
password2 = getpass.getpass('Password (again): ')
if password == password2:
break
sys.stderr.write("Error: Your passwords didn't match.\n")
except KeyboardInterrupt:
sys.stderr.write("\nOperation cancelled.\n")
sys.exit(1)
u = users.create_user(username, email, password)
u.is_staff = True
u.is_active = True
u.is_superuser = True
u.save()
print "User created successfully."
createsuperuser.args = ''
def runserver(port):
"Starts a lightweight Web server for development."
from django.core.servers.basehttp import run, WSGIServerException