Making with Code

Model, Databases, & Admin Portal #

During the track lessons you will be creating a web app for the ISF Riddler. You should remember the Riddle database from the Networking Unit!


[0] Starter Code #

๐ŸŒ Github Repo: github.com/Making-with-Code/lab_riddler_backend

๐Ÿ’ป Install requirements
poetry install
๐Ÿ’ป Enter the poetry shell.
poetry shell

[1] The Model #

๐Ÿ‘€ You can find the Riddle model in riddle_app/models.py.

๐Ÿ’ป Firstly, let’s migrate our Riddle model into the database. Then, run the server.

python manage.py migrate
python manage.py runserver   `

๐Ÿ’ป Visit 127.0.0.1:8000/ to view the app! As you are in the backend track, you already have html and css files.


[2] Admin Portal #

Just like other parts of our app, the Administration page is a route that is configured in Django. With Django’s default project setup, the panel is automatically enabled.

As you can see in riddle_app/admin.py the Riddle model is registered.

from django.contrib import admin
from .models import Riddle

admin.site.register(Riddle)

๐Ÿ’ป We can now access the admin portal by going to: 127.0.0.1:8000/admin/.

If you go to this URL, there will be a webpage where we can log in to the admin site. We need Superuser access to log in.


[Creating a Superuser Account] #

We’ve successfully checked that the admin site is ready to go but we can’t log in! We need to create a Superuser account to log into the admin site.

๐Ÿ’ป Create a SuperUser to access the admin portal. You’ll need to quit the server, then create the user. Remember, control + d quits the server.

python manage.py createsuperuser

Django will then prompt you to enter a username and password and that user will have superuser access to the admin site. For ease of use, keep your username and password short. You can also skip the email field with return. It will just ask you type y to Bypass password validation and create user.

It can be as simple as

  • username: a
  • password: 123

๐Ÿ’ป Now, restart the server and login to the admin portal: 127.0.0.1:8000/admin/

๐Ÿ’ป Explore the portal and add 3 riddles

๐Ÿ‘พ ๐Ÿ’ฌ God View

As the owners and administrators of your website, you have access to your users’ accounts and all of your users’ data. We literally have a God view of everything that is entered into our database.

There are lots of ethical questions that need to be considered when building an app, and because of this, always remember the quote from Aunt May to Peter Parker…

With great power comes great responsibility!


[3] Django Shell #

Now that we have some data in our database, let’s see what we can do with it! We can try out different methods using the python shell.

๐Ÿ’ป Enter the shell. You will need to quit the server to enter the shell.

python manage.py shell

๐Ÿ’ป First, we can use the built-in capabilities of any Django model object. You can read the official Django Model Documentation to learn more.

๐Ÿ’ญ You should remember this from the Networking unit!

>>> from riddle_app.models import Riddle
>>> Riddle.objects.all()
>>> Riddle.objects.count()
3
>>> Riddle.objects.first()
<Riddle: Iโ€™m the rare case when today comes before yesterday. What am I?>
>>> Riddle.objects.last()
<Riddle: I speak without a mouth and hear without ears. I have no body, but I come alive with wind. What am I?>
>>> Riddle.objects.get(id=1)
<Riddle: Iโ€™m the rare case when today comes before yesterday. What am I?>
๐Ÿ’ป We can try printing out some of the Task properties
>>> first_riddle = Riddle.objects.first()
>>> first_riddle.answer
'A question of time'
>>> first_riddle.question
'Iโ€™m the rare case when today comes before yesterday. What am I?'
>>> first_riddle.likes
47

[Riddle Methods] #

๐Ÿ‘€ The Riddle model has 2 methods:

  • check_guess()
  • increase_likes()

๐Ÿ’ป Try and successfully use both methods in the shell. One way to know if you were successful, is to check the admin portal and see if the likes property increases for a specific Riddle.


[4] Changing the Model #

What if we decide we want to add more fields and methods to our Riddle model. It’s possible with makemigrations.

๐Ÿ’ป Make the following additions to the Riddle in riddle_app/models.py:

0. Add a field for number of guesses - which method should you incorporate this addition?

1. Add a field for correct guesses - which method should you incorporate this addition?

2. Add a field for hints - in a future lab we will include this into your NewRiddleForm


๐Ÿ’ป Once you’ve changed the code, you will need to change the database with makemigrations.

python manage.py makemigrations
python manage.py migrate
python manage.py runserver     

โ˜‘๏ธ Now, let’s login to the admin portal to confirm your changes were made: 127.0.0.1:8000/admin/

โ˜‘๏ธ Also, don’t forget to use the shell to check if your changes to the methods were successful


[5] Deliverables #

โšกโœจ

๐Ÿ’ป Push your work to Github:

  • git status
  • git add -A
  • git status
  • git commit -m "your message goes here"
    • be sure to customize this message, do not copy and paste this line
  • git push