Python 3x Pandas Django

Django Admin Interface


This tutorial begins where our previous tutorial session basic request and response left off. In this tutorial session, we’ll setup the database, create your first model, and get a quick introduction to Django’s automatically-generated admin site.

Database Setup

Now, open up myfirstsite/settings.py. It’s a normal Python module with module-level variables representing Django settings.

By default, the configuration uses SQLite. If you’re new to databases, or you’re just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won’t need to install anything else to support your database. When starting your first real project, you may want to use a more scalable database like PostgreSQL,Mysql , Oracle to avoid database-switching headaches down the road.

Copied below lines from myfirstsite/settings.py, Django default configurations uses SQLite 'django.db.backends.sqlite3'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

If you wish to use another database, install the appropriate database bindings and change the following keys in the databases 'default' item to match your database connection settings:

• ENGINE – 'django.db.backends.sqlite3', 'django.db.backends.postgresql', 'django.db.backends.mysql', or 'django.db.backends.oracle' or some other engines are available.

• NAME – The name of your database. If you’re using SQLite, the database will be a file on your computer; in that case, NAME should be the full absolute path, including filename, of that file. The default value, BASE_DIR / 'db.sqlite3', will store the file in your project directory.

If you are not using SQLite as your database, additional settings such as USER, PASSWORD, and HOST must be added.

For Example, PostgreSQL: (We will see each DBs connection parameters in our upcoming tutorial sessions)

When you connecting to PostgreSQL database additional connection parameters will be required something similar to the below one,

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

Also, note the INSTALLED_APPS setting at the top of the setting.py file. That holds the names of all Django applications that are activated in this Django instance. Apps can be used in multiple projects, and you can package and distribute them for use by others in their projects.

By default, INSTALLED_APPS contains the following apps, all of which come with Django:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Copied above lines from myfirstsite/settings.py, Django comes with some default apps.

These applications are included by default as a convenience for the common case.

Some of these applications make use of at least one database table, though, so we need to create the tables in the database before we can use them. To do that, run the following command:

python manage.py migrate

The migrate command looks at the INSTALLED_APPS setting and creates any necessary database tables according to the database settings in your myfirstsite/settings.py file and the database migrations shipped with the app (we’ll cover those later). You’ll see a message for each migration it applies. Let’s check our command prompt after running comment python manage.py migrate

You’ll see the following output on the command line:

(tutorial1-env) C:\Users\user\tutorial1>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

Re-start your development server by running command python manage.py runserver and you will see the following output on the command line.

(tutorial1-env) C:\Users\user\tutorial1>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 30, 2021 - 17:43:10
Django version 3.2.6, using settings 'myfirstsite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

Notice below errors are gone now because we ran python manage.py migrate as it required for DB’s migrations:

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser. You’ll see a “Hello, World.” Page. It worked!

Introducing the Django Admin

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site.

The admin interface isn’t intended to be used by site visitors. It’s for site managers\admin who manage your site.

Creating an admin user,

We’ll need to create a user who can login to the admin site by running the following command: python manage.py createsuperuser

Go to your command prompt quit the server with CTRL-BREAK and run the following command:

python manage.py createsuperuser

image

Enter your desired username and press enter. (I have added admin as Username)

image

You will then be prompted for your desired email address: (It’s n0t a mandatory field so I left as Blank)

image

The final step is to enter your password. You will be asked to enter your password twice, the second time as a confirmation of the first.

image

Start the development server by running the following command:

python manage.py runserver

Now, open a Web browser and go to “/admin/” on your local domain – http://127.0.0.1:8000/admin/. You should see the admin’s login screen:

image

And try logging in with the superuser account you created in the previous step. You should see the Django admin index page:

image

You should see a few types of editable content: groups and users. They are provided by django.contrib.auth, the authentication framework shipped by Django.

Congratulations! You have successfully completed Django admin site tutorial session. Let us move to the next topic Django Models.

If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!