CS50web — Lecture 4 SQL,Models and Migrations (part 2)

Maggie
6 min readJan 13, 2022

Admin app

application that Django done to help you to manipulate the model quicker
under airline → urls.py

Admin account need to be created before we use it → How?

Terminal: python manage.py createsuperuser

After creating the admin account:

Go to admin under app and import models

Tell admin app that I would like to manipulate Airport and Flight models
How it works?

go to the admin url

login

You can edit the table in admin

Also for flight

Customize the web application so that user can view details when you click on the flight — every flight to have its own page

OR

if you use pk ← ignore the name of what it is being called

What if the id is not exist in the table?

Deal with it later

Why flight to airport is not a many-to-many relationship?
1. 1 origin/destination only link to 1 airport (one to one?)
2. 1 airport link to many 1 origin/destination (one to many?)

Why flight to passenger is a many to many relationship?
1. 1 flight could be related to more than 1 passenger (use primary key as reference)
2. 1 passenger could be related to more than 1 flight

reference: https://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561

Add passengers to the flight

As passengers to flight is a many to many relationship: use function ManyToManyField

Go to admin.py under flight → add the model to admin

if you could add more than 1 options here, it will be at least x to many relationship.
if you want to select more than 1 customers in flight, it will be many to many relationship

Go to view to pass information from passenger

Go to flight.html to include the passenger information

Add in link to go back to index and route

Allow the passengers to book a flight

Whenever you would like to manipulate the database, should be in POST method.

update flight.html

Admin interphase could be customized to do what you want to do

This will change the display of table in admin

You could check documentation for more information: https://docs.djangoproject.com/en/3.0/ref/contrib/admin/

Tutor’s favorite display for many to many relationship:

Users- authentication

Start with creating new app: users

When the app was named as users: user ←attribute already assigned

above function: tell you that the user is authenticated or not

should be login_view/ logout_view

update the login.html and layout.html

Then go to admin to add in users

Further information could be edited here

After logging out admin, try to go to user

as no login yet, so I should be redirected to login

then update if I successfully login

useful!!! If there is variable message pass through → show the message

When I login with wrong username:

Now need to edit the page when user successfully login the page:

how to get the user name?

request.user.first_name ← inside django, you have access to the HTTP request ← also the user and first_name

How to logout the user?

Directly applying the logout function with the django built-in function is super helpful

--

--