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

View File

@ -24,6 +24,18 @@ application and will focus on Django's automatically-generated admin site.
The admin isn't necessarily intended to be used by site visitors; it's for site
managers.
Create a user account
=====================
Run the following command to create a superuser account for your admin site::
django-admin.py createsuperuser --settings="myproject.settings.main"
(Note: You can use either "myproject.settings.main" or "myproject.settings.admin"
here. They both reference the same database.)
The script will prompt you for a username, e-mail address and password (twice).
Start the development server
============================
@ -49,28 +61,6 @@ admin's login screen:
.. image:: http://media.djangoproject.com/img/doc/tutorial/admin01.png
:alt: Django admin login screen
Create a user account
=====================
You can't log in, though, because you haven't created an admin user account
yet. Drop into the Python interactive interpreter and type this::
# The function django.models.auth.users.create_user() creates a new user
# and returns the new auth.User object.
# Don't use 'username' and 'password'. Those are just examples.
>>> from django.models.auth import users
>>> u = users.create_user('username', 'your_email@domain.com', 'password')
# But we're not done. We need to explicitly set is_staff and is_active to
# allow this user to access the admin. Might as well make it a superuser,
# too.
u.is_staff = True
u.is_active = True
u.is_superuser = True
# Remember, call the save() method to save changes.
u.save()
Enter the admin site
====================