In this fourth part we need to create a super user and make migrations. Once we have a username and a password we can connect to our admin panel and create todo items.
Step 1 – Make migrations and migrate
Before we can do anything in our admin panel we have to make migrations and migrate. This will create the tables in the database. This is easily done by running the following command in the terminal:
ddnro:~/workspace/todoapp $ python3 manage.py makemigrations Migrations for 'todo': todo/migrations/0001_initial.py - Create model TodoItem
The newly created model is easily detected and the migration is made. Nothing is changed though until the migrations are migrated. To migrate the migrations you have to run the command
python3 manage.py migrate:
ddnro:~/workspace/todoapp $ python3 manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions, todo 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 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 sessions.0001_initial... OK Applying todo.0001_initial... OK
That’s it! Two simple commands and the migrations are made and applied. Now we can go ahead and create a super user.
Step 2 – Create a super user
The super user is created easily via the terminal by running the following command:
ddnro:~/workspace/todoapp $ python3 manage.py createsuperuser Username (leave blank to use 'ubuntu'): admin Email address: firstname.lastname@example.org Password: Password (again): Superuser created successfully.
You can choose whatever username and email you want. The password has to be at least 8 characters long. Now we should be able to login to our admin panel. Run the server and go to the url
admin. Type in the username and password you choose above and you should be in the admin panel.
There it is, the Todo item! Now you can create todo items via admin panel. You can also edit, mark as done, and delete them as well. Go ahead and create a few todo items.
This is it for part 4. In part 5 we are going to create an HTML template that is going to be our template for all the other HTML files.