|
| 1 | +.. _django-get-started-create-admin: |
| 2 | + |
| 3 | +==================== |
| 4 | +Create an Admin Site |
| 5 | +==================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: tutorial |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: app, odm, code example |
| 13 | + |
| 14 | +You can create a Django admin site to edit your application's |
| 15 | +data from a web interface. To learn more about the Django admin |
| 16 | +site and its features, see `The Django admin site <https://docs.djangoproject.com/en/{+django-version-number+}/ref/contrib/admin/>`__ |
| 17 | +in the Django documentation. |
| 18 | + |
| 19 | +.. procedure:: |
| 20 | + :style: connected |
| 21 | + |
| 22 | + .. step:: Create an admin user |
| 23 | + |
| 24 | + Before creating an admin site, you must create a user |
| 25 | + who can log in to the site. |
| 26 | + |
| 27 | + From your project's root directory, run the following command to create |
| 28 | + an admin user: |
| 29 | + |
| 30 | + .. code-block:: bash |
| 31 | + |
| 32 | + python manage.py createsuperuser |
| 33 | + |
| 34 | + Then, your terminal prompts you for a username, email address, |
| 35 | + and password. For each prompt, enter the following information |
| 36 | + to create a user with the specified credentials and press "enter" |
| 37 | + after each entry: |
| 38 | + |
| 39 | + .. code-block:: bash |
| 40 | + :copyable: false |
| 41 | + |
| 42 | + Username: admin |
| 43 | + |
| 44 | + Password: <admin-password> |
| 45 | + Password (again): <admin-password> |
| 46 | + |
| 47 | + Replace the ``<admin-password>`` placeholder with your user's password. |
| 48 | + |
| 49 | + .. step:: Enter the admin site |
| 50 | + |
| 51 | + Run the following code to start your server: |
| 52 | + |
| 53 | + .. code-block:: bash |
| 54 | + |
| 55 | + python manage.py runserver |
| 56 | + |
| 57 | + Once your server is running, visit the http://127.0.0.1:8000/admin/ |
| 58 | + URL to see the admin site. This site displays the following login |
| 59 | + screen: |
| 60 | + |
| 61 | + .. figure:: /includes/figures/django_admin_login.png |
| 62 | + :alt: The login screen on the Django admin page. |
| 63 | + |
| 64 | + Enter the username and password created in the previous step to log in to |
| 65 | + the site. |
| 66 | + |
| 67 | + .. step:: Access your "sample_mflix" app from the admin site |
| 68 | + |
| 69 | + After logging in to the admin site, you can see the following information: |
| 70 | + |
| 71 | + .. figure:: /includes/figures/django_admin_index.png |
| 72 | + :alt: The initial content displayed on the Django admin site. |
| 73 | + |
| 74 | + You can edit your project's authentication configuration by selecting |
| 75 | + the :guilabel:`Groups` or :guilabel:`Users` row in the |
| 76 | + :guilabel:`Authentication and Authorization` table. |
| 77 | + |
| 78 | + To edit the data in the ``users`` sample collection, represented by your |
| 79 | + ``Viewer`` model, navigate to your project's ``sample_mflix/admin.py`` file |
| 80 | + and paste the following code: |
| 81 | + |
| 82 | + .. code-block:: python |
| 83 | + |
| 84 | + from django.contrib import admin |
| 85 | + |
| 86 | + from .models import Viewer |
| 87 | + |
| 88 | + admin.site.register(Viewer) |
| 89 | + |
| 90 | + Now, your admin site displays the following information: |
| 91 | + |
| 92 | + .. figure:: /includes/figures/django_admin_model.png |
| 93 | + :alt: The content displayed on the Django admin site after registering a model. |
| 94 | + |
| 95 | + .. step:: Select a Viewer object |
| 96 | + |
| 97 | + You can view the data stored in a ``Viewer`` object that |
| 98 | + has a ``name`` value of ``"Abigail Carter"``. You created |
| 99 | + this object in the :ref:`django-get-started-write` step |
| 100 | + of this tutorial. |
| 101 | + |
| 102 | + Click on the :guilabel:`Viewers` row of the :guilabel:`SAMPLE_MFLIX` |
| 103 | + table to see a list of viewers. The admin site displays the following |
| 104 | + list: |
| 105 | + |
| 106 | + .. figure:: /includes/figures/django_admin_viewers.png |
| 107 | + :alt: The list of viewers displayed on the admin site. |
| 108 | + |
| 109 | + Then, click on :guilabel:`Abigail Carter` at the top of the list. |
| 110 | + The site displays the :guilabel:`Name` and :guilabel:`Email` of |
| 111 | + the selected viewer: |
| 112 | + |
| 113 | + .. figure:: /includes/figures/django_admin_edit_viewer.png |
| 114 | + :alt: The information of your selected viewer. |
| 115 | + |
| 116 | + .. step:: Edit the data in a Viewer object |
| 117 | + |
| 118 | + To edit the ``email`` field of the viewer, select the |
| 119 | + box that contains the text ``" [email protected]"``. |
| 120 | + Delete this text and replace it with ``" [email protected]"``, |
| 121 | + as shown in the following image: |
| 122 | + |
| 123 | + .. figure:: /includes/figures/django_admin_new_email.png |
| 124 | + :alt: The updated email address of your viewer. |
| 125 | + |
| 126 | + Then, click the :guilabel:`SAVE` button below the viewer's |
| 127 | + information to save your changes. |
| 128 | + |
| 129 | +After completing these steps, you can access the Django |
| 130 | +admin site and use it to edit your ``Viewer`` objects. |
0 commit comments